angluar 路由初学踩坑

1.初学文档,跟到官网尝试学习到路由时

    报错 提示: Uncaught Error: Template parse errors: 'router-outlet' is not a known element:

自己查资料,找到了解决办法

    网址:https://stackoverflow.com/questions/44517737/router-outlet-is-not-a-known-element

代码:

        使用的是下面指令:

        ng generate module app-routing --flat --module=app

        and update the app-routing.ts file to add:

        @NgModule({

  imports: [ RouterModule.forRoot(routes) ],

  exports: [ RouterModule ]

})

Here are the full example:

import { NgModule }            from '@angular/core';

import { RouterModule, Routes } from '@angular/router';

import { DashboardComponent }  from './dashboard/dashboard.component';

import { HeroesComponent }      from './heroes/heroes.component';

import { HeroDetailComponent }  from './hero-detail/hero-detail.component';

const routes: Routes = [

  { path: '', redirectTo: '/dashboard', pathMatch: 'full' },

  { path: 'dashboard', component: DashboardComponent },

  { path: 'detail/:id', component: HeroDetailComponent },

  { path: 'heroes', component: HeroesComponent }

];

@NgModule({

  imports: [ RouterModule.forRoot(routes) ],

  exports: [ RouterModule ]

})

export class AppRoutingModule {}

and add AppRoutingModule into app.module.ts imports:

@NgModule({

  declarations: [

    AppComponent,

    ...

  ],

  imports: [

    BrowserModule,

    FormsModule,

    AppRoutingModule

  ],

  providers: [...],

  bootstrap: [AppComponent]

})

            

你可能感兴趣的:(angluar 路由初学踩坑)