如果你在Angular 8中遇到了路由重写失败的问题,可以尝试以下解决方法:
@angular/router
模块,可以通过运行以下命令来安装它:npm install @angular/router
app.module.ts
)中导入RouterModule
和Routes
:import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppModule { }
app.component.html
)中添加带有router-outlet
指令的占位符,用来展示路由组件:
import { Component } from '@angular/core';
@Component({
selector: 'app-home',
template: 'Welcome to Home Page
',
})
export class HomeComponent { }
http://localhost:4200/home
。如果按照上述步骤操作后仍然无法解决问题,可能需要检查其他可能的原因,例如路由配置是否正确、导入路径是否正确等。此外,你还可以查看浏览器的控制台输出,以获取更多关于路由问题的信息。