【发布时间】:2019-08-11 09:59:23
【问题描述】:
我正在尝试使用我的路由器加载我的 Angular 组件,但它从未显示或引发错误。
应用路由模块
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{
path: 'home', component: HomeComponent, children:
[
{ path: 'compare', component: CompareComponent, children:[] },
{
path: 'settings', component: SettingsComponent, children: [
{ path: 'new', component: ServerConnectionEditComponent }
]
},
]
},
应用服务器连接
constructor(private router:Router, private route:ActivatedRoute) { }
onAddServer()
{
console.log(this.route.url)
this.router.navigate(['new'], {relativeTo: this.route});
}
该 URL 似乎有效,因为任何其他 URL 都会引发错误。
应用服务器连接编辑
ngOnInit() {
console.log("in Edit") //never gets here
}
服务器连接.component.html
...
<th scope="col"><button type="button" class="btn btn-success btn-sm"
(click)="onAddServer()">+</button></th>
app-component.html
<app-header></app-header>
<div class="container">
<div class="row">
<div class="col-md-12">
<router-outlet></router-outlet>
</div>
</div>
</div>
settings.component.html
<div class="container">
<div class="row">
<div>
<div class="col-md-12" >
<h4>Server Connections:</h4>
</div>
<br/>
<div class="col-xs-12">
<app-server-connections [serverConnections]="serverConnections"></app-server-connections>
</div>
</div>
</div>
</div>
【问题讨论】:
-
您要导航到哪个网址?
-
<router-outlet>的设置够了吗?请记住,当使用children时,每个父组件都需要在其模板中添加一个<router-outlet>。 -
如果我添加
组件会立即加载,并且我希望它在按钮单击时加载。
标签: javascript angular typescript angular8