且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

角度网址匹配器-匹配以开头的任何路径

更新时间:2022-12-15 12:46:04

下面是解决方案,尝试一下.

Here is the solution try this..

    import { Component } from '@angular/core';
    import { Router } from "@angular/router";
    import { Event as NavigationEvent } from "@angular/router";

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      public url: string;
      constructor(router: Router) {

        router.events.subscribe((event: NavigationEvent): void => {
            this.url = router.url;
            if (this.url.match('/tree/')) {          
              console.log("Matched your Path",this.url);
            }
          }
        );

      }
    }

您可以在此处更改匹配的部分

you can change your matching part is here

 if(this.url.match('give your matching here')){
       //do something
 }

您可以在this.url变量中获取捕获的值.

you get the captured value in this.url variable.

它的工作非常完美.希望能对您有所帮助.

Its worked perfect. I hope its help.