我试图在路由改变时从路由器获取数据,但我没有成功.我在这里设置了asdf属性
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent,
LoginComponent,
DashboardComponent,
OverviewComponent,
],
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot([
{ path: '', pathMatch: 'full', redirectTo: '' },
{ component: LoginComponent, path: 'login' },
{
children: [
{ path: '', pathMatch: 'full', redirectTo: 'overview', data: { asdf: 'hello' } },
{ component: OverviewComponent, path: 'overview', data: { asdf: 'hello' } },
], component: DashboardComponent,
path: '',
},
]),
],
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
在这里,我可以在路由更改时从路由器获取URL但asdf未定义:(
import { Component, OnDestroy, …Run Code Online (Sandbox Code Playgroud) 这个我花了一段时间才弄明白,所以我希望通过在 SO 的问答方式中分享解决方案来节省某人的时间。它是这样的:
我有一个 Angular8 Web 应用程序,我使用RouterModule在组件之间导航。
以下是我的路线的定义方式:
const routes: Routes = [
{ path: 'moo', component: AppMooComponent, data: { title: 'Moo Section', desc: 'Moo is the first example component.' } },
{ path: 'boo', component: AppBooComponent, data: { title: 'Boo Section', desc: 'Boo is the second example component.' } },
{ path: 'foo', component: AppFooComponent, data: { title: 'Foo Section', desc: 'Foo is the third example component.' } },
{ path: '', redirectTo: 'moo', pathMatch: 'full' …Run Code Online (Sandbox Code Playgroud)