在Angular 8中,可以使用默认路由来指定应用程序的默认页面。下面是一个示例解决方法:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' }, // 默认路由
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'contact', component: ContactComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
完整的app.module.ts文件示例代码:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home.component';
import { AboutComponent } from './about.component';
import { ContactComponent } from './contact.component';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' }, // 默认路由
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'contact', component: ContactComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
请确保在路由数组中定义了与默认路由相匹配的组件,并将相应的组件导入到app.module.ts文件中。
这样,当用户访问应用程序的根URL时,会自动重定向到默认路由指定的组件。