简体   繁体   English

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

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

I'm trying to use RouterLink but nothing happens.我正在尝试使用 RouterLink,但没有任何反应。

I have two components, the category and the savecategory.我有两个组件,类别和保存类别。 the savecategory is defined in the app.module in the declarations part and the category has its own module. savecategory 在 app.module 的声明部分中定义,并且类别有自己的模块。 so I'm importing your module into imports所以我将你的模块导入到导入中

App.module应用模块

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

app-routing应用路由

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' }
];

Category.module类别.模块

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

I'm using the following router to go to login screen我正在使用以下路由器进入登录屏幕

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

works perfectly in the savecategory but not in the category.在保存类别中完美运行,但在类别中不起作用。

Is there any missing import in the category?类别中是否缺少任何导入?

The routes array of routes describes how to navigate.路由的路由数组描述了如何导航。 Pass it to the RouterModule.forRoot method in the module imports to configure the router.将其传递给模块导入中的 RouterModule.forRoot 方法以配置路由器。

I included the routes const in the app.module.ts我在 app.module.ts 中包含了路由 const

app.module.ts (excerpt): 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]
})

Docs文档

The Categories Module requires you to import the RouterModule for the 'routerLink' directive to work in your template.类别模块要求您为“routerLink”指令导入 RouterModule 以在您的模板中工作。

Hope it helps希望能帮助到你

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

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

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