【问题标题】:Angular 2 Routing cannot match any routesAngular 2 Routing 无法匹配任何路由
【发布时间】:2017-03-28 19:06:25
【问题描述】:

我正在尝试重新路由到登录时的其他页面,但由于某种原因,我的路由似乎不起作用。它一直告诉我它在我的路由器中“无法匹配任何路由”。我显然有那里的路线,并且我尝试了各种方法使其与路径匹配。也许另一双眼睛可以看到我看不到的东西。

AppRouter.module:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { LoginComponent } from '../Components/login.component';
import { MenuComponent } from '../Components/menu.component';

const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'menu', component: MenuComponent }
];

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

login.component.module:

import { Component } from '@angular/core';
import { Router } from  '@angular/router';

@Component({
  templateUrl: '../Templates/login.component.html'
})
export class LoginComponent {
  title = 'app works!';
  username: string = 'user';
  password: string = 'password'

  constructor(private router: Router){

  }

  validate(): void{
    if(this.username === "user" &&
      this.password === "password"){
      console.log("Logged In!");
      this.router.navigate(['../main']);
    }
    else {
          console.log(this.username + ' ' + this.password);
    }
  }
}

导入到我的模块中就好了,menu.component 超级基础,没有错别字。如果我在标签内添加“路由链接”,它将打开,但没有其他方式。任何想法都会很棒,谢谢。

【问题讨论】:

  • 您的main 路线在哪里列出?我在routes 中没有看到它。

标签: angular routing angular2-routing


【解决方案1】:

我认为你拼错了……

这是在您的路线配置中:

{ path: 'menu', component: MenuComponent }

这是你的组件

this.router.navigate(['../main']);

两个不同的词:menu 和 main

第二种情况的另一个问题可能是两个点。您应该使用一个,因为它是同一级别(两个点表示“多一级”)

【讨论】:

  • 我现在很生气!!!哈哈。谢谢伙计,我是这里唯一的开发人员,我知道这将是一件小事。
【解决方案2】:

将this.router.navigate(['../main']); 更改为this.router.navigate(['./menu']);。您不希望路由器上升一个级别,因为这两个组件是从[host]/login 和[host]/menu 提供的,它们在层次结构上处于同一级别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2019-01-16
    • 2017-01-21
    • 2018-01-05
    • 2016-10-01
    • 2022-01-25
    相关资源
    最近更新 更多