在Angular 6中,我如何访问app.component.ts中的路由值



我是angular 6的新手。我已经使用angularjs很多年了。我正在努力制定应用程序的路由和结构。我想能够访问app.compnent中的路由值。我在这里有我的网站模板,基于路由参数,我需要能够更改主题。我有一个名为modulePath的路由参数,它将始终存在于所有路由中。由于app.component包含路由器出口,尽管它似乎无法访问路由值。如有任何帮助,我们将不胜感激。也许我做这件事的方式完全错了。

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, NavigationStart, NavigationEnd} from 
'@angular/router';
import { Location } from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'Ang';
modulePath: string;
private sub: any;
constructor(
private route: ActivatedRoute,
router: Router,
private location: Location
) {
}
ngOnInit(): void {
this.sub = this.route.params.subscribe(params => {
this.modulePath = params['modulePath'];
console.log(params);
});
}
}

对于我的特定问题,我找到了解决方案。我不知道你可能在另一个路由器出口已经显示的组件中有一个路由器插座。对我来说,我现在有一个父路线,它有模块名称并设置了网站的主题。

这是另一篇文章的链接。这里给出的答案显示了如何在路线中创建路线。Angular 2,RC5路由器出口在另一个路由器出口内

最新更新