'router-outlet' is not a known element

后端 未结 12 1137
无人及你
无人及你 2021-01-30 19:36

I have a mvc 5 project with a angular frontend . I wanted to add routing as described in this tutorial https://angular.io/guide/router. So in my _Layout.cshtml I ad

12条回答
  •  星月不相逢
    2021-01-30 19:46

    There are two ways. 1. if you want to implement app.module.ts file then:

    import { Routes, RouterModule } from '@angular/router';
    
    const appRoutes: Routes = [
      { path: '', component: HomeComponent },
      { path: 'user', component: UserComponent },
      { path: 'server', component: ServerComponent }
    ];
    
    @NgModule({
      imports: [
        RouterModule.forRoot(appRoutes)
      ]
    })
    export class AppModule { }

    1. if you want to implement app-routing.module.ts (Separated Routing Module) file then:

    //app-routing.module.ts
    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    
    const appRoutes: Routes = [
      { path: '', component: HomeComponent },
      { path: 'users', component: UsersComponent },
      { path: 'servers', component: ServersComponent }
    ];
    
    @NgModule({
      imports: [
        RouterModule.forRoot(appRoutes)
      ],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
    
    //................................................................
    
    //app.module.ts
    import { AppRoutingModule } from './app-routing.module';
    
    @NgModule({
      imports: [
        AppRoutingModule
      ]
    })
    export class AppModule { }

提交回复
热议问题