首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子路由错误

子路由错误
EN

Stack Overflow用户
提问于 2018-02-09 12:19:10
回答 3查看 110关注 0票数 1

我在我的app.module.ts中创建了以下内容:

代码语言:javascript
复制
const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'login', component: LoginComponent },
  { path: 'register', component: SignUpComponent },
  { path: 'about', component: AboutUsComponent },
  { path: 'products', component: ProductsComponent},
  { path: 'fresh-food', component: FreshFoodComponent, 
    children: [
      {path: 'milks-creams', component: MilkCreamComponent},
      {path: 'cheeses', component: CheeseComponent}
    ] },
  { path: '', redirectTo: '/home', pathMatch: 'full' },    // whenever path is empty --> redirect
  { path: '**', redirectTo: '/home', pathMatch: 'full' }   // if path is anything not defined --> redirect

]

并在my @NgModule中添加了以下导入

代码语言:javascript
复制
imports: [
    BrowserModule,
    RouterModule.forRoot(appRoutes),
    FormsModule,
    HttpClientModule
  ],

添加子路由后,在浏览器中收到以下错误:

代码语言:javascript
复制
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'home'
Error: Cannot match any routes. URL Segment: 'home'
    at ApplyRedirects.noMatchError (router.js:1719)
    at CatchSubscriber.eval [as selector] (router.js:1705)
    at CatchSubscriber.error (catchError.js:105)

..。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-09 12:22:11

问题在于:

代码语言:javascript
复制
const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: '', redirectTo: '/home', pathMatch: 'full' },    // whenever path is empty --> redirect
  { path: '**', redirectTo: '/home', pathMatch: 'full' }   // if path is anything not defined --> redirect
]

您试图将用户重定向到路由中未声明的“home”路径。

你不想写信吗?

代码语言:javascript
复制
  { path: 'home', component: HomeComponent },

或者你想这样做?

代码语言:javascript
复制
const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: '**', redirectTo: '/', pathMatch: 'full' }   // if path is anything not defined --> redirect
]
票数 1
EN

Stack Overflow用户

发布于 2018-02-09 12:23:49

在您的退出信息'home'未定义。

请将此添加到您的rout信息中如下:

代码语言:javascript
复制
{ path: 'home', component: HomeComponent }

更改rout信息的这一部分:

代码语言:javascript
复制
{ path: '', redirectTo: '/', pathMatch: 'full' }, 
{ path: '**', redirectTo: '/', pathMatch: 'full' }
票数 2
EN

Stack Overflow用户

发布于 2018-02-09 12:23:29

解决方案1: Home /home

将代码更改为

代码语言:javascript
复制
const appRoutes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'login', component: LoginComponent },
  { path: 'register', component: SignUpComponent },
  { path: 'about', component: AboutUsComponent },
  { path: 'products', component: ProductsComponent},
  { path: 'fresh-food', component: FreshFoodComponent, 
    children: [
      {path: 'milks-creams', component: MilkCreamComponent},
      {path: 'cheeses', component: CheeseComponent}
    ] },
  { path: '', redirectTo: '/home', pathMatch: 'full' },    // whenever path is empty --> redirect
  { path: '**', redirectTo: '/home', pathMatch: 'full' }   // if path is anything not defined --> redirect

]

当没有提到路径时,您将路由重定向到path '/home',因此必须为'/home‘而不是'’提到组件HomeComponent

解决方案2: Home /

如果您想让家在“/”上,那么不要使用重定向,并将代码保持为

代码语言:javascript
复制
const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'login', component: LoginComponent },
  { path: 'register', component: SignUpComponent },
  { path: 'about', component: AboutUsComponent },
  { path: 'products', component: ProductsComponent},
  { path: 'fresh-food', component: FreshFoodComponent, 
    children: [
      {path: 'milks-creams', component: MilkCreamComponent},
      {path: 'cheeses', component: CheeseComponent}
    ] }

]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48705698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档