繁体   English   中英

angular routerLink 在带有模块的组件中不起作用

[英]angular routerLink does not work in component with module

我正在尝试使用 RouterLink,但没有任何反应。

我有两个组件,类别和保存类别。 savecategory 在 app.module 的声明部分中定义,并且类别有自己的模块。 所以我将你的模块导入到导入中

应用模块

@NgModule({
  declarations: [
    AppComponent,
    SavecategoriaComponent // <~ savecategory
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    HttpModule,
    HttpClientModule,
    LoginModule,
    CategoriaModule,           // <~~CategoryModule
    NgbModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})

应用路由

const routes: Routes = [
  { path: 'login', component: LoginComponent },  
  { path: 'categoria', component: CategoriaComponent },
  { path: 'categoria/:id', component: CategoriaComponent },
  { path: 'savecategoria', component: SavecategoriaComponent },
  { path: '', pathMatch: 'full', redirectTo: 'login' }
];

类别.模块

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    FormsModule,   
    HttpClientModule 
  ],
  declarations: [
    CategoriaComponent
  ]
})

我正在使用以下路由器进入登录屏幕

<a routerLink="/">Home</a>

在保存类别中完美运行,但在类别中不起作用。

类别中是否缺少任何导入?

路由的路由数组描述了如何导航。 将其传递给模块导入中的 RouterModule.forRoot 方法以配置路由器。

我在 app.module.ts 中包含了路由 const

app.module.ts(摘录):

import { RouterModule } from '@angular/router';
....

const routes: Routes = [

  { path: 'login', component: LoginComponent },  
  { path: 'categoria', component: CategoriaComponent },
  { path: 'categoria/:id', component: CategoriaComponent },
  { path: 'savecategoria', component: SavecategoriaComponent },
  { path: '', pathMatch: 'full', redirectTo: 'login' }
];

@NgModule({
      declarations: [
        AppComponent,
        SavecategoriaComponent // <~ savecategory
      ],
      imports: [
        BrowserModule,
        FormsModule,
        RouterModule.forRoot(routes), //here for instance add it
        HttpModule,
        HttpClientModule,
        LoginModule,
        CategoriaModule,           // <~~CategoryModule
        NgbModule.forRoot()
      ],
        providers: [],
  bootstrap: [AppComponent]
})

文档

类别模块要求您为“routerLink”指令导入 RouterModule 以在您的模板中工作。

希望能帮助到你

如果一切顺利,应该可以工作,检查类别模块的路由,很可能没有处理该链接的路由。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM