【问题标题】:Angular routing from one component to another从一个组件到另一个组件的角度路由
【发布时间】:2018-10-18 08:43:13
【问题描述】:

我有一个名为 main 的组件,它具有此处定义的 main.routing.ts

@NgModule({
  imports: [
    RouterModule.forRoot([
      { path: 'main',
        component: MainComponent,
        canActivate: [AuthGuard],
        children: [{ path: '', component: AccountMainComponent },
      { path: 'financial-accounts', component: FinancialAccountsComponent },
      { path: 'system-config', component: ConfigSysComponent },
      { path: 'conciliacao', component: ConciliacaoContabilComponent },
      { path: 'report', component: ReportComponent },
      ]}
    ])
  ]
 })
 export class MainRouting { }

我还有另一个content-toolbar 组件,我想在其中创建指向main 组件的链接。 content-toolbar 在 content-toolbar.module.tscontent-toolbar.component.ts 处导入 router。我在content-toolbar.component.html 有这段代码:

<span>
    <a routerLink="/main">
      <img src="/assets/icons/logo_white.svg" class="logo">
    </a>
</span>

但是这段代码并没有把这张图片变成另一个组件的链接。我应该如何从content-toolbar 引用main 要路由的组件?

【问题讨论】:

  • 你有路由器插座吗?没有它,就没有导航。
  • @LordAlpaca,我应该在哪里添加路由器出口标签?我的尝试最终没有呈现网站
  • 你能添加你声明content-toolbar.component的模块吗?
  • @GabrielRado,你应该在你的层次结构中的某个地方有路由器插座,也许是 app.component.html。只需输入&lt;router-outlet&gt;&lt;/router-outlet&gt; 并尝试一下。

标签: angular routing angular-router angular-routerlink


【解决方案1】:

因此,要使 Angular 路由正常工作,需要注意以下事项

  1. 在路由模块或 app.module.ts 中导入 RouterModule。
  2. 使用 forroot 在导入部分声明/配置您的路由。
  3. 导出 RouteModule 以便它在整个应用程序中可用。
  4. 在路由模块或 app.module.ts 中导入组件。
import { RouterModule } from '@angular/router';
//import your components

@NgModule({
  declarations:[your components],//your components needs to declared here.
  imports: [
    RouterModule.forRoot([
      { path: 'main',
        component: MainComponent,
        canActivate: [AuthGuard],
        children: [{ path: '', component: AccountMainComponent },
      { path: 'financial-accounts', component: FinancialAccountsComponent },
      { path: 'system-config', component: ConfigSysComponent },
      { path: 'conciliacao', component: ConciliacaoContabilComponent },
      { path: 'report', component: ReportComponent },
      ]}
    ]) //this defines/configures all the routes of the application
  ],
exports: [RouterModule] //this will export RouterModule through which we will be able to discover our routes
 })
 export class MainRouting { }

现在我们的路由模块已准备好处理请求。现在我们还有 2 件事待处理。

  1. 调用路由
<span>
    <a routerLink="/main">
      <img src="/assets/icons/logo_white.svg" class="logo">
    </a>
</span>
  1. 使用路由器出口指定需要加载路由的位置。在 Angular 中,我们首先要了解 2 个页面是 index.html。这是将加载所有内容的着陆页或页面。

接下来,app.component.html 是我们的根组件,它非常适合加载路由。只需在文件中的任何位置添加以下代码,只要它在文件中即可。

<router-outlet></router-outlet>

现在我们应该可以很好地导航黑白组件了。

参考:https://angular.io/tutorial/toh-pt5#add-routeroutlet

学习 Angular 先试试这个项目:https://angular.io/tutorial

【讨论】:

    【解决方案2】:

    我会猜一猜并说明以下内容:

    你很可能在一个看起来像这样的模块中声明ContentToolbarComponent

    @NgModule({
    imports: [..., MainRoutingModule,...],
    declarations: [..., ContentToolbarComponent, ...]
    })
    export class FooModule {}
    

    问题在于您可能将导出RouterLinkDirectiveRouterModule 导入FooModule。这就是为什么 Angular 不会生成带有超链接的锚元素。

    解决方案是将RouterModule 添加到MainRoutingModule 的导出中,如下所示:

    @NgModule({
      imports: [
        RouterModule.forRoot([
          { path: 'main',
            component: MainComponent,
            canActivate: [AuthGuard],
            children: [{ path: '', component: AccountMainComponent },
          { path: 'financial-accounts', component: FinancialAccountsComponent },
          { path: 'system-config', component: ConfigSysComponent },
          { path: 'conciliacao', component: ConciliacaoContabilComponent },
          { path: 'report', component: ReportComponent },
          ]}
        ])
      ],
      exports: [RouterModule]
     })
     export class MainRouting { }
    

    重要的是,声明工具栏组件的模块必须直接导入RouterModule或通过exports另一个 imported 模块,以便工具栏组件可以使用该指令。

    【讨论】:

    • 虽然你的回答很有道理,但我没能完成这项工作。我注意到我的content-toolbar.module 没有导入MainRoutingModule,也没有像你所说的那样声明ContentToolbarComponent。抱歉我缺乏知识,但这些是强制性的吗?
    • 你需要将RouterModule导入到声明ContentToolbarComponent的模块中,仅此而已。您只需要将 MainRoutingModule 导入您的根模块。如果您使用我在 cmets 中要求的信息更新您的答案,我可能会更具体。
    • 我注意到声明ContentToolbarComponent的模块有错误后成功了。感谢您的耐心等待
    【解决方案3】:

    你必须像这样将 routerLinkActive="active" 添加到 a 标签:

    <span>
        <a routerLink="/main" routerLinkActive="active">
          <img src="/assets/icons/logo_white.svg" class="logo">
        </a>
    </span>
    

    编辑 路由器对象也应该有 pathmatch: 'full':

    children: [{ path: '', component: AccountMainComponent, pathMatch: 'full' }
    

    【讨论】:

    • 我做了,但没用。您对我可能遗漏的内容或我应该显示的任何额外代码有任何想法吗?
    • @GabrielRado 您必须将“pathMatch:'full'”添加到子路径:''
    • 不,检查我得到的元素&lt;a routerlink="main" routerlinkactive="active"&gt; &lt;img class="logo" src="/assets/icons/logo_white.svg"&gt; &lt;/a&gt;。标签应该小写吗?
    • 试试这样的大写:"routerLink", "routerLinkActive"
    • @Jota.Toledo 我的错,不需要
    猜你喜欢
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2023-04-10
    • 1970-01-01
    • 2018-07-11
    • 2018-09-20
    • 2019-10-10
    相关资源
    最近更新 更多