问题描述: 当使用Angular路由导航到某个URL时,却发现打开了错误的组件。
解决办法:
const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: '**', component: PageNotFoundComponent } ];
const routes: Routes = [ { path: 'product/:id', component: ProductDetailComponent } ];
@Component({ selector: 'app-product-detail', templateUrl: './product-detail.component.html', styleUrls: ['./product-detail.component.css'] })
@NgModule({ declarations: [ AppComponent, HomeComponent, AboutComponent, PageNotFoundComponent ], imports: [ BrowserModule, RouterModule.forRoot(routes) ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
遵循这些步骤后,您应该能够解决路由导航到错误组件的问题。