我有两个链接和url我动态获取ngOnInit
。routerLinkActive动态路由默认为活动Angular 2
<li><a [routerLink]="welcomeUrl" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }">Getting Started</a></li>
<li><a [routerLink]="dashboardUrl" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }">Dashboard</a></li>
这里是我的组件:
ngOnInit() {
this.dataService.getOptions().subscribe((options: Options) => {
if (options.mode.toLowerCase() === "live") {
this.dashboardUrl = '/';
this.welcomeUrl = 'welcome';
} else {
this.dataService.getName().subscribe((name: string) => {
this.dashboardUrl = name;
this.welcomeUrl = name + '/welcome';
});
}
});
}
和路线:
const routes: Routes = [
{ path: '', component: DashboardComponent },
{ path: 'welcome', loadChildren: 'app/welcome/welcome.module#WelcomeModule' },
{ path: ':name', component: DashboardComponent },
{ path: ':name/welcome', loadChildren: 'app/welcome/welcome.module#WelcomeModule' },
];
但是当我启动应用这些链接是有效的。点击更改网址链接后,他们开始正常工作。
默认情况下这些链接(url)是空的并且始终处于活动状态?我该如何解决这个问题?
我已修复,现在我没有任何活动链接,但dashboardUrl(“/”)应该处于活动状态。 –
我认为你的语法不对。请看看这个https://angular.io/docs/ts/latest/guide/template-syntax.html#!#property-binding我没有看到任何使用,如你所说。 –
您将* property binding *(您发送的链接)与* Angular指令*(例如'routerLink')混淆在一起。绑定到DOM属性,例如'',与使用Angular指令不一样。你可以通过包含你的路线声明来更新你的问题吗?我认为混乱来自什么'welcomeUrl'和'dashboardUrl'代表... – AngularChef