[英]Angular Child Route not changing
I am designing a website with angular4, and have a component that inside uses the @angular/material sidenav. 我正在设计一个带有angular4的网站,并且有一个内部使用@ angular / material sidenav的组件。 Within that sidenav i have
<router-outlet></router-outlet>
. 在那边,我有
<router-outlet></router-outlet>
。
my app.module has all the necessary imports and declarations, I have a app-routing module that contains the following code for the component: 我的app.module具有所有必要的导入和声明,我有一个app路由模块,其中包含该组件的以下代码:
const appRoutes: Routes = [
{ path: 'reg',
component: RegComponent,
children: [
{ path: ':info', component: RegInfoComponent },
{ path: ':ther', component: RegTherComponent }
]},
];
I have two span objects to open the sidenav: <span class="label label-info" [routerLink]="['/reg', 'info']" (click)=sidenav.open() style="cursor: pointer;">Info</span>
我有两个span对象来打开sidenav:
<span class="label label-info" [routerLink]="['/reg', 'info']" (click)=sidenav.open() style="cursor: pointer;">Info</span>
and <span class="label label-info" [routerLink]="['/reg', 'ther']" (click)=sidenav.open() style="cursor: pointer;">Ther</span>
和
<span class="label label-info" [routerLink]="['/reg', 'ther']" (click)=sidenav.open() style="cursor: pointer;">Ther</span>
The sidenav opens fine, but only loads the html template from RegInfoComponent, if i click the ther
link, it still loads the html template from RegInfoComponent, but the URL does change. 该sidenav打开罚款,但只加载从RegInfoComponent HTML模板,如果我点击
ther
链接,它仍然加载从RegInfoComponent HTML模板,但URL不会改变。
At first i thought it was maybe because its inside the angular material sidenav, so i placed the <router-outlet></router-outlet>
below the link, and it still wouldnt change. 起初,我认为可能是因为它位于有角度的材质sidenav内,所以我将
<router-outlet></router-outlet>
放置在链接下方,并且仍然不会更改。
I'm not sure what i've done wrong that the router-outlet is not displaying the correct child information, hoping somebody can help with this? 我不确定我做错了什么,即路由器插座未显示正确的子信息,希望有人可以提供帮助吗? Thanks.
谢谢。
The code for RegInfoComponent: RegInfoComponent的代码:
import { ActivatedRoute, Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-reg-info',
templateUrl: './reg-info.component.html',
styleUrls: ['./reg-info.component.css']
})
export class RegInfoComponent implements OnInit {
constructor(route: ActivatedRoute,
router: Router) { }
ngOnInit() {
}
}
and the code for the RegTherComponent 以及RegTherComponent的代码
import { ActivatedRoute, Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-reg-ther',
templateUrl: './reg-ther.component.html',
styleUrls: ['./reg-ther.component.css']
})
export class RegTherComponent implements OnInit {
constructor(route: ActivatedRoute,
router: Router) { }
ngOnInit() {
}
}
I think your router config is incorrect 我认为您的路由器配置不正确
you are using variable segments with :info
and :ther
so the first one ( :info
) will always match first (and the RegInfoComponent gets instantiated), and therefore RegTherComponent
will never be instantiated 您将变量段与
:info
和:ther
因此第一个( :info
)将始终首先匹配(并且RegInfoComponent被实例化),因此RegTherComponent
将永远不会被实例化
You need to do something like this 你需要做这样的事情
<span class="label label-info" [routerLink]="['/reg/info']" (click)=sidenav.open() style="cursor: pointer;">Info</span>
<span class="label label-info" [routerLink]="['/reg/ther']" (click)=sidenav.open() style="cursor: pointer;">Ther</span>
and then the router config should be 然后路由器配置应该是
const appRoutes: Routes = [
{ path: 'reg',
component: RegComponent,
children: [
{ path: 'info', component: RegInfoComponent },
{ path: 'ther', component: RegTherComponent }
]},
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.