29

我在 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

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

我在我的代码中做错了什么?任何帮助都会有帮助谢谢

4

4 回答 4

28

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

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

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

<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 { }
于 2018-08-26T17:44:05.870 回答
8

如果您需要[]语法,当您需要通过路由传递id等参数时,对“编辑表单”很有用,您可以执行以下操作:

[routerLink]="['edit', business._id]"

至于没有像你这样的参数的“关于页面”,

[routerLink]="/about"

或者

[routerLink]=['about']

会成功的。

于 2019-02-07T17:52:28.973 回答
3

正如错误所说,您的路由器链接应与配置的现有路由匹配

应该只是 routerLink="/about

于 2018-08-26T17:47:02.237 回答
0

我通过搜索很多天找到了正确的答案;只需转换 routerLink="['/jkh',id]"

[routerLink]="['/jkh',id]"

并给它所有的请求和解决方案;

于 2022-01-07T13:14:31.103 回答