【问题标题】:How to set up my routing that "pathMatch" and childRoutingModule work?如何设置“pathMatch”和 childRoutingModule 工作的路由?
【发布时间】:2019-10-01 12:05:39
【问题描述】:
  1. 在左侧菜单中,当用户点击链接时,我需要突出显示链接。现在它可以工作了,但我的菜单的根路径“li”也被突出显示。

  2. 我无法设置子路由模块。

左侧菜单 html

<ul class='side-menu'>
  <li *ngFor='let item of menu' routerLinkActive='active-side-menu-item'><a routerLink='{{ item.link }}' class="menu-item-feed">{{ item.title }}</a></li>
</ul>

app.routing.module ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { FeedComponent } from './feed/feed.component';

const routes: Routes = [
  { path: '', pathMatch: 'full', component: FeedComponent },
  { path: 'shoes', pathMatch: 'full', component: FeedComponent },
  { path: 'coats', pathMatch: 'full', component: FeedComponent },
  { path: 'shirts', pathMatch: 'full', component: FeedComponent },
  { path: 'pants', pathMatch: 'full', component: FeedComponent },
  { path: 'item/:id', component: FeedComponent },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

这就是我尝试实现 childRoutingModule 的方式,但它在控制台中抛出错误:core.js:15724 ERROR Error: Uncaught (in promise): TypeError: undefined is not a function 类型错误:未定义不是函数 在 Array.map()

app.routing.module

const routes: Routes = [
  { path: '', pathMatch: 'full', loadChildren: './feed/feed.module#FeedModule' }
];

feed-routing.module

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { FeedComponent } from './feed.component';

const routes: Routes = [
  { path: '', pathMatch: 'full', component: FeedComponent },
  { path: 'shoes', pathMatch: 'full', component: FeedComponent },
  { path: 'coats', pathMatch: 'full', component: FeedComponent },
  { path: 'shirts', pathMatch: 'full', component: FeedComponent },
  { path: 'pants', pathMatch: 'full', component: FeedComponent },
  { path: 'item/:id', component: FeedComponent },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class FeedRoutingModule { }

我想为网站上的每个总部分设置一个单独的路由模块,我需要链接对 url 做出反应并指出哪个模块目前处于活动状态。

如果有任何其他提示和最佳做法,我将不胜感激!

【问题讨论】:

    标签: node.js angular typescript ecmascript-6 single-page-application


    【解决方案1】:

    要设置功能路由,您必须将 FeedRoutingModule 导入要使用该路由的模块中。

    下面是我的例子

    我有 app.routing.ts(主路由)

    export const routes: Routes = [
      { path: "", redirectTo: "home", pathMatch: "full" },
      { path: "home", component: HomeComponent },
    
      //{ path: "**", redirectTo: "account", pathMatch: "full" },
      //lazy load feature module
      { path: "account", loadChildren: "./account/account.module#AccountModule" },
      { path: "portal", loadChildren: "./portal/portal.module#PortalModule", canActivate: [AuthGuardService] },
      { path: "**", redirectTo: "account", pathMatch: "full" },
    ];
    

    和 app.module.ts

    @NgModule({
      declarations: [],
      imports: [
        RouterModule.forRoot(routes)
      ],
      providers: [],
      bootstrap: []
    })
    export class AppModule {}
    

    所以当我在我的功能模块中定义子路由时,我必须像 app.module 一样,但使用 RouterModule.forChild

    export const accountRoutes: Routes = [
      {
        path: "",
        component: AccountComponent,
        children: [
          { path: "login", component: LoginComponent },
          { path: "register-account", component: RegisterComponent }
        ]
      }
    ];
    
    
    import { NgModule } from "@angular/core";
    import { CommonModule } from "@angular/common";
    import { FormsModule, ReactiveFormsModule } from "@angular/forms";
    import { RouterModule } from "@angular/router";
    
    import { RegisterComponent } from "./register/register.component";
    import { LoginComponent } from "./login/login.component";
    import { AccountComponent } from "./account/account.component";
    import { accountRoutes } from "./account.routing";
    
    @NgModule({
      declarations: [],
      imports: [
        RouterModule.forChild(accountRoutes) // use forChild
      ],
      exports: [],
      providers: []
    })
    export class AccountModule {}
    

    【讨论】:

    • 实际上,除了子路径之外,我所做的一切都是一样的,但我怀疑这是问题所在,因为在没有子路径的情况下它对我有用。你没注意到我在哪里犯了错误吗?
    • 谢谢你,我的朋友!最后,我发现了我的错误。起初我没有明白你的意思,但现在我知道我将 FeedMudule 添加到了我的 App.module 是什么导致了这个错误。我不应该在那里添加它:S显然
    • 是的,一开始我并没有注意到你没有在 App.module 中导入 CustomModule,但今天我打开了我以前的一个项目,瞥了一眼我是如何实现路由的在那里,我注意到我没有导入自定义模块:D 然后我回到你的代码并注意到你也没有导入它们。昨天我检查了很多代码,看不出我的代码和你的代码有什么区别。我希望我不会再犯这种愚蠢的错误 XD 为了解决这种愚蠢的错误而浪费了一天时间
    【解决方案2】:

    你的paranet组件需要有标签:

    然后他知道会有一个子组件+路由;

    这是我管理 childRoutes 的示例。

    { 路径:'utente', 组件:CenterViewComponent, 孩子们: [ YYY_ROUTE, XXX_ROUTE ... ] }

    希望能帮到你!

    【讨论】:

    • 如我所见,您在 app.routing.module 中使用 children 属性实现了 childRouting,对吗?我实际上没有尝试过这种方式。但有趣的是,如果它适用于我的情况。我在根目录中有“router-outlet”标签。但是 loadChildren 不起作用。 :/ 你不知道为什么吗?我敢打赌这是一个非常愚蠢的错误,因为我以前做过很多次,但这次它不起作用?
    • 是的,我都在一个地方。每个组件都有自己的路由文件。从'@angular/router'导入{路由};从“。”导入{ MainRegisterComponent }; export const MAIN_REGISTER_ROUTE: Route = { path: 'main-register', component: MainRegisterComponent };然后我将它导入我的主要路线。你可以在这里生孩子。
    • 我想了解为什么我在使用单独的路由模块时遇到该错误,我在主 app.routing.module 中 loadChildren
    • 感谢您的回答,我的朋友!我终于找到了我的错误:D
    • 这么愚蠢的错误丢了一天))这样的日子不是那么鼓舞人心
    猜你喜欢
    • 2018-05-24
    • 2017-05-05
    • 1970-01-01
    • 2019-08-01
    • 2022-07-19
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 2018-01-14
    相关资源
    最近更新 更多