我在我的应用程序路由模块中定义了一些路由数据,如下所示:
const appRoutes:Routes = [
{path: '', component: LoginComponent, data:[{PageName:"Login Page"}]}]
Run Code Online (Sandbox Code Playgroud)
我想全局获取数据,所以我使用app.component.ts来获取URL重定向相关信息,如下所示:
export class AppComponent {
title = 'Welcome';
constructor(public router: Router, public authenticationService: AuthenticationService) {
this.router.events.subscribe(event => {
console.log("Url",event.urlAfterRedirects);
console.log("Page Name", router[PageName]); //Not working
}
Run Code Online (Sandbox Code Playgroud)
我尝试注入ActivatedRoute,但每次重定向后都没有更新.
无论如何,我可以在哪里配置页面名称并在全球范围内获取它app.component.ts.
问题:在路由中定义的静态数据永远不会通过订阅 ActivatedRoute 的数据对象来检索。其他一切似乎都正常,数据对象不为空,但我无法从中获取数据。当我尝试从数据对象调试数据时,它输出“未定义”,当我尝试将它绑定到 UI 时,没有任何显示,但是当我查看 Chrome 中的 ActivatedRoute 消息时,它有数据。经过多次尝试,我很确定我的语法应该基于许多示例工作,所以 Angular 6 中的某些内容可能发生了变化,或者 Angular 有什么问题?
路线代码:
const appRoutes: Routes = [
{
path: "article",
redirectTo: "/article/partners",
pathMatch: "full"
},
{
path: "article",
children: [
{
path: "bawo",
component: BawoArticleComponent,
data: { title: 'BaWo' }
},
{
path: "goldenhands",
component: GoldenHandsArticleComponent,
data: { title: 'Golden Hands' }
},
{
path: "investors",
component: InvestorsArticleComponent,
data: { title: 'Investors' }
},
{
path: "partners",
component: PartnersArticleComponent,
data: { title: 'Partners' }
}
]
}, …Run Code Online (Sandbox Code Playgroud)