且构网

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

Angular url matcher - 匹配任何以开头的路径

更新时间:2023-11-26 18:16:28

这是解决方案试试这个..

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.