子路由仅在重定向时有效



在Angular中使用子路由时,我遇到了一个有趣的问题。只有当重定向到一个"不工作"的路由时,路由才会起作用。路线。

在我的浏览器中,如果我输入localhost:4200/customer/add/,我得到404 Not Found错误。

但是如果我输入localhost:4200/customer(这个路由被重定向到customer/add),页面就会正常工作。

我传送模式:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';
import { AuthenticationLayoutComponent } from '../../components/authentication-layout/authentication-layout.component';
import { LoginComponent } from 'src/app/components/authentication/login/login.component';
import { AddCustomerComponent } from 'src/app/components/customer/add-customer/add-customer.component';
import { RegisterComponent } from 'src/app/components/authentication/register/register.component';
import { MasterLayoutComponent } from 'src/app/components/master-layout/master-layout.component';
const routes: Routes = [
  {
    path: 'customer',
    component: MasterLayoutComponent,
    children: [
      { path: "", redirectTo: 'add',pathMatch: 'full' },
      { path: "add", component: AddCustomerComponent},
    ],
  },
  {
    path: '',
    component: AuthenticationLayoutComponent,
    children: [
      { path: 'login', component: LoginComponent },
      { path: 'register', component: RegisterComponent },
    ],
  },
];
@NgModule({
  declarations: [],
  providers: [],
  imports: [
    CommonModule,
    RouterModule.forRoot(routes, { enableTracing: false }),
  ],
  exports: [RouterModule],
})
export class AppRoutingModule {}

我认为问题出在下面这行代码上:

{ path: "", redirectTo: 'add',pathMatch: 'full' },

你可以试着删除它。

最新更新