[英]Cannot match any routes angular 13
I started learning angular and ran into a routing problem there are following routing modules in app-routing.module.ts:我开始学习角度并遇到路由问题,app-routing.module.ts 中有以下路由模块:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './p/home/home.component';
import { WorksComponent } from './p/works/works.component';
import { CenzorComponent } from './p/works/cenzor/cenzor.component';
import { UserlistComponent } from './p/works/userlist/userlist.component';
import { TasklistComponent } from './p/works/tasklist/tasklist.component';
const routes: Routes = [
{path:'',redirectTo:'home',pathMatch:'full'},
{ path:'home',component: HomeComponent},
{ path:'',component: WorksComponent,children:[
{path:'',redirectTo:'works',pathMatch:'full'},
{ path:'cenzor',component: CenzorComponent},
{ path:'userlist',component: UserlistComponent},
{ path:'tasklist',component: TasklistComponent},
] },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.component.html app.component.html
<app-header></app-header>
<router-outlet></router-outlet>
app.module.ts app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header.component';
import { HomeComponent } from './p/home/home.component';
import { WorksComponent } from './p/works/works.component';
import { CenzorComponent } from './p/works/cenzor/cenzor.component';
import { UserlistComponent } from './p/works/userlist/userlist.component';
import { TasklistComponent } from './p/works/tasklist/tasklist.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
HomeComponent,
WorksComponent,
CenzorComponent,
UserlistComponent,
TasklistComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
home.component.html home.component.html
<div class="container-fluid d-flex justify-content-around">
<div class="card" style="width: 18rem;">
<img src="../../assets/images/1.jpg" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title text-danger text-center">list of prohibited words</h5>
<a class="btn btn-success" routerLink="censor">censor</a>
</div>
</div>
<div class="card" style="width: 18rem;">
<img src="../../assets/images/2.jpg" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title text-danger text-center">angular task list</h5>
<a class="btn btn-success" routerLink="tasklist">tasklist</a>
</div>
</div>
<div class="card" style="width: 18rem;">
<img src="../../assets/images/3.jpg" class="card-img-top" alt="...">
<div class="card-body bg-light">
<h5 class="card-title text-danger text-center">Angular users list</h5>
<a class="btn btn-success " routerLinkActive="active" routerLink="userlist">userlist</a>
</div>
</div>
</div>
I think there is problem with routes, because i get following error in console: ERROR Error: Uncaught (in promise): Error: Cannot match any routes.我认为路由有问题,因为我在控制台中收到以下错误:错误错误:未捕获(承诺中):错误:无法匹配任何路由。 URL Segment: 'home/userlist'.
URL 段:'home/userlist'。 Anyway i dont know how to handle this Thank You!
无论如何,我不知道如何处理这个谢谢!
There are several issues with your routes set-up.您的路线设置存在几个问题。 I'll try to clarify.
我会尽力澄清。
{path:'',redirectTo:'home',pathMatch:'full'},
In this, you are saying that when someone goes to url.com/
they get redirected to url.com/home
which is all good so far.在这里,您是说当有人访问
url.com/
时,他们会被重定向到url.com/home
,到目前为止一切都很好。
In here:在这里:
{ path:'home',component: HomeComponent},
you load homeComponent.你加载 homeComponent。 All good as well.
一切都很好。 But then:
但是之后:
{ path:'',component: WorksComponent,children:[
{path:'',redirectTo:'works',pathMatch:'full'},
{ path:'cenzor',component: CenzorComponent},
{ path:'userlist',component: UserlistComponent},
{ path:'tasklist',component: TasklistComponent},
] }
In here you are telling the router that ''
path will load the WorksComponent
, but this will never happen because of the first mentioned thing (you reroute this path to home
, so user can never get here).在这里,您告诉路由器
''
path 将加载WorksComponent
,但这永远不会发生,因为第一个提到的事情(您将此路径重新路由到home
,因此用户永远无法到达这里)。
Next, you are adding children to this path.接下来,您将向此路径添加子级。 So even if you could access this component, you would need to add a
<router-outlet>
into the markdown of WorksComponent
.因此,即使您可以访问此组件,您也需要将
<router-outlet>
添加到WorksComponent
的降价中。
You are trying to access home/userlist
which makes me realize that you want to:您正在尝试访问
home/userlist
,这让我意识到您想要:
HomeComponent
, that way, this component is the one with access to all paths after it ( url.com/home/*
), so if you add userlist as a child of HomeComponent
you could route url.com/home/userlist
, right now this path doesn't exist.HomeComponent
,这样,该组件就可以访问它之后的所有路径( url.com/home/*
),因此如果将 userlist 添加为HomeComponent
的子项,则可以路由url.com/home/userlist
,现在这条路径不存在。<router-outlet>
to your HomeComponent
's html.<router-outlet>
添加到HomeComponent
的 html 中。 Inside this router outlet is where the userlist
component will render.userlist
组件将呈现的地方。 So if you do <h1>This is home</h1>
<router-outlet></router-outlet>
<p>Some text goes here</p>
Then the userlist
component will render between the title, and the text.然后用户
userlist
组件将在标题和文本之间呈现。 If that makes sense.如果这是有道理的。 Also reroute your
WorksComponent
, it is incongruent for a route to have a redirectTo
and a component load at the same time.还要重新路由您的
WorksComponent
,路由同时具有redirectTo
和组件加载是不一致的。
Fransisco explained well, and if I want to sum it up, first you should be careful about order of routes because Router uses a first-match wins strategy when matching routes, so more specific routes should be placed above less specific routes. Fransisco 解释的很好,如果我想总结一下,首先你应该注意路由的顺序,因为 Router 在匹配路由时使用的是先匹配获胜策略,所以更具体的路由应该放在不太具体的路由之上。 Second probably need to use / before any paths in your html file like this: router link="/userlist".
其次可能需要在 html 文件中的任何路径之前使用 /,如下所示:router link="/userlist"。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.