Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用角路由器获取URL_Javascript_Angular_Typescript_Angular Router_Angular8 - Fatal编程技术网

Javascript 使用角路由器获取URL

Javascript 使用角路由器获取URL,javascript,angular,typescript,angular-router,angular8,Javascript,Angular,Typescript,Angular Router,Angular8,我需要在一个组件(我的导航组件)中获取我的当前路由(使用路由器),该组件位于应用程序组件中,但由于它已经加载,它不会在单击时刷新,导航组件中的我的函数也不会返回任何新的URL。 如何将新URL与导航组件一起使用 这是我的应用程序组件: 这是我的导航组件(.ts): 当我的应用程序导航出现在每个组件中时,它都在工作,但自从我将其移动后,它就不再工作了。您需要订阅ActivatedRoute服务 大概是这样的: 添加激活的路由: constructor( protected r

我需要在一个组件(我的导航组件)中获取我的当前路由(使用路由器),该组件位于应用程序组件中,但由于它已经加载,它不会在单击时刷新,导航组件中的我的函数也不会返回任何新的URL。 如何将新URL与导航组件一起使用

这是我的应用程序组件:


这是我的导航组件(.ts):


当我的应用程序导航出现在每个组件中时,它都在工作,但自从我将其移动后,它就不再工作了。

您需要订阅ActivatedRoute服务

大概是这样的: 添加激活的路由:

 constructor(
        protected route: ActivatedRoute,
        ...) {
   }
添加订阅:

this.route.url.subscribe(value => {
     ....
    });

您可以使用可观察的路由器服务事件

应用程序组件

NavigationEnd导航成功结束时触发的事件


您的导航组件它不包括在您的路线中,因此您无法获取当前路线,我建议您在app.component中获取当前根目录,并将其作为参数传递给NavBari如果我将ngOnInit移动到nav.component.ts,同一问题:页面更改,没有返回新的URL..在my app component或nav one中?@GermainWeb您需要添加订阅服务器,在何处运行if。我猜是在nav组件中。正在使用.events,但在NavigationEnd上出现了一个引用错误:NavigationEnd不是defined@GermainWeb添加导入声明了吗?你搞定了!
this.route.url.subscribe(value => {
     ....
    });
import {
  ActivatedRoute
} from '@angular/router';

export class AppComponent implements OnInit {
  constructor(private route: ActivatedRoute) {
    console.log("current route is " + route);
  }
}
  constructor( public router: Router,) {
  }

  public ngOnInit(): void {
    this.router.events.subscribe(e => {


      if (e instanceof NavigationEnd) {
        console.log(this.router.url);
        // ... 
      }

    });
  }