相关疑难解决方法(0)

如何在Angular2中订阅router.events.subscribe时从Route或ActivatedRoute获取数据?

我试图在路由改变时从路由器获取数据,但我没有成功.我在这里设置了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)

angular-ui-router angular

10
推荐指数
4
解决办法
1万
查看次数

如何在包含 RouterOutlet 的 HTML 模板中直接使用激活路由的数据?

这个我花了一段时间才弄明白,所以我希望通过在 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)

angular2-routing angular angular8

7
推荐指数
1
解决办法
1497
查看次数