gpt4 book ai didi

javascript - ERROR 错误 : Uncaught (in promise), 无法匹配任何路由。网址段

转载 作者:技术小花猫 更新时间:2023-10-29 12:40:33 24 4
gpt4 key购买 nike

我在 Angular 6 应用程序中有简单的导航,

这是HTML

<nav class="main-nav>
<ul class="main-nav__list " ng-sticky [addClass]="'main-sticky-link'" [ngClass]="ref.click === true? 'Navbar__ToggleShow' :''">
<li class="main-nav__item">
<a class="main-nav__link" routerLink="['/']" routerLinkActive="active">Home</a>
</li>
<li class="main-nav__item">
<a class="main-nav__link" routerLink="['/about']" routerLinkActive="active">About us</a>
</li>

</ul>
</nav>

这里是app.routing模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { MainLayoutComponent } from './layout/main-layout/main-layout.component';
import { AboutComponent } from './components/about/about.component';
import { WhatwedoComponent } from './components/whatwedo/whatwedo.component';
import { FooterComponent } from './components/footer/footer.component';
import { ProjectsComponent } from './components/projects/projects.component';
const routes: Routes = [
{ path: 'about', component: AboutComponent },
{ path: 'what', component: WhatwedoComponent },
{ path: 'contacts', component: FooterComponent },
{ path: 'projects', component: ProjectsComponent},
];

@NgModule({
imports: [
CommonModule,
RouterModule.forRoot(routes),
],
declarations: []
})
export class AppRoutingModule { }

这是应用程序模块

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgStickyDirective } from 'ng-sticky';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { MainLayoutComponent } from './layout/main-layout/main-layout.component';
import { AppRoutingModule } from './/app-routing.module';
import { MainNavDirective } from './layout/main-nav.directive';
import { AboutComponent } from './components/about/about.component';
import { WhatwedoComponent } from './components/whatwedo/whatwedo.component';
import { FooterComponent } from './components/footer/footer.component';
import { WhyChooseUsComponent } from './components/why-choose-us/why-choose-us.component';
import { TeamComponent } from './components/team/team.component';
import { ProjectsComponent } from './components/projects/projects.component';
import { ClientsComponent } from './components/clients/clients.component';
import { HowItWorksComponent } from './components/how-it-works/how-it-works.component';
import { PartnersComponent } from './components/partners/partners.component';

@NgModule({
declarations: [
AppComponent,
NgStickyDirective,
MainLayoutComponent,
MainNavDirective,
AboutComponent,
WhatwedoComponent,
FooterComponent,
WhyChooseUsComponent,
TeamComponent,
ProjectsComponent,
ClientsComponent,
HowItWorksComponent,
PartnersComponent
],
imports: [
BrowserModule,
RouterModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

当我运行我的应用程序并点击关于我们时,出现以下错误:

core.js:1673 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: '%5B'/about'%5D'
Error: Cannot match any routes. URL Segment: '%5B'/about'%5D'
at

我尝试了不同的组合来解决这个问题,但仍然无法摆脱这个错误,

我的代码哪里做错了?任何帮助都会有帮助谢谢

最佳答案

当你像这样使用 routerLink 时,你需要传递它应该去的路由的值。但是,当您将 routerLink 与属性绑定(bind)语法一起使用时,如下所示:[routerLink] , 那么应该为它分配一个属性名称,该属性的值将是它应该将用户导航到的路径。

因此,要解决您的问题,请替换此 routerLink="['/about']"routerLink="/about"在您的 HTML 中。

在其他地方,您可以在不需要时使用属性绑定(bind)语法。我已经修复了它,您可以简单地使用下面的模板语法:

<nav class="main-nav>
<ul
class="main-nav__list"
ng-sticky
addClass="main-sticky-link"
[ngClass]="ref.click ? 'Navbar__ToggleShow' : ''">
<li class="main-nav__item" routerLinkActive="active">
<a class="main-nav__link" routerLink="/">Home</a>
</li>
<li class="main-nav__item" routerLinkActive="active">
<a class="main-nav__link" routerLink="/about">About us</a>
</li>
</ul>
</nav>

它还需要知道应该在哪里加载与它到达的路由对应的组件的模板。所以为此,不要忘记添加 <router-outlet></router-outlet> ,在上面提供的模板或父组件中。

您的 AppRoutingModule 还有另一个问题.您需要导出 RouterModule从那里以便您可以使用它 AppModule当它导入它时。要解决此问题,请将其从您的 AppRoutingModule 中导出通过将其添加到 exports数组。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { MainLayoutComponent } from './layout/main-layout/main-layout.component';
import { AboutComponent } from './components/about/about.component';
import { WhatwedoComponent } from './components/whatwedo/whatwedo.component';
import { FooterComponent } from './components/footer/footer.component';
import { ProjectsComponent } from './components/projects/projects.component';
const routes: Routes = [
{ path: 'about', component: AboutComponent },
{ path: 'what', component: WhatwedoComponent },
{ path: 'contacts', component: FooterComponent },
{ path: 'projects', component: ProjectsComponent},
];

@NgModule({
imports: [
CommonModule,
RouterModule.forRoot(routes),
],
exports: [RouterModule],
declarations: []
})
export class AppRoutingModule { }

关于javascript - ERROR 错误 : Uncaught (in promise), 无法匹配任何路由。网址段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52028782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com