可能是由于缺少重定向功能而导致的。可以尝试在应用根路径的路由定义中设置 pathMatch 属性,并将其设置为 'full'。这将确保路由匹配完整路径,在页面刷新时也可以正确加载 router-outlet。
示例代码:
// app-routing.module.ts import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component';
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 AppRoutingModule { }
请注意,在根路径路由中设置 pathMatch 属性非常重要。这将在页面刷新时确保正确加载 router-outlet。
希望这可以帮助您解决问题。