【问题标题】:EXCEPTION: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'details'例外:未捕获(承诺):错误:无法匹配任何路由。 URL 段:“详细信息”
【发布时间】:2017-06-16 04:54:56
【问题描述】:

我有这个路由器:

import { Route, RouterModule } from '@angular/router';
import { ProjectDetailsComponent } from '../components/project-details/project-details.component';

export const ROUTES: Route[] = [
  {
    path: '',
    redirectTo: '/details',
    pathMatch: 'full'
  },
  {
    path: 'details/:id',
    component: ProjectDetailsComponent
  }
];

我还有一个合适的控制器,名为 ProjectDetailsComponent。

这是我的应用模块,我在其中注册了路线:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';

// Custom components
import { AppComponent } from './app.component';
import { ProjectDetailsComponent } from './components/project-details/project-details.component';

// Routes
import { ROUTES } from './services/router';

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(ROUTES, { useHash: true })
  ],
  declarations: [AppComponent, ProjectDetailsComponent],
  bootstrap: [AppComponent]
})
export class AppModule {
}

它是我的 app.component,我尝试使用“/details”路由重定向到 ProjectDetailsComponent 并将项目 ID 发送给它:

import { Component, OnInit } from '@angular/core';
import { IProject } from './domain/projects
import { Router } from '@angular/router';
// Routes
import { ROUTES } from './services/router';

@Component({
    selector: 'projects-app',
    templateUrl: 'app/app.component.html'
})
export class AppComponent implements AfterViewInit {
    public activeProject: IProject;
    public projects: Array<IProject>;

    constructor(public router: Router) {
    }

    ngOnInit() {
        this.router.navigate(['/details', this.activeProject.Id]);
    }
}

但是当我尝试导航到 'details/:id' 路由器时,我收到以下错误:

C:\Sources\SolarPro\build\debug\resources\app\node_modules\@angular\core\bundles\core.umd.js:3521 EXCEPTION: Uncaught (in promise): 
   Error: Cannot match any routes. URL Segment: 'details'

请帮我解决我的问题。

【问题讨论】:

  • 如果你直接浏览到&lt;server_url&gt;/details/1 可以吗?
  • 我在电子应用程序中使用 Angular2,但我不知道如何检查这种方式。如果我尝试使用window.location.assing() 更改位置,它不会按预期工作。但是如果我使用this.router.navigateByUrl("/app/details/1");,我会得到之前的错误。

标签: angular typescript routing components router


【解决方案1】:

我遇到了同样的问题,所有可能的解决方案,但最后,这解决了我的问题

export class AppRoutingModule {
constructor(private router: Router) {
    this.router.errorHandler = (error: any) => {
        this.router.navigate(['details']); // or redirect to default route
    }
  }
}

这会起作用,因为 Angular 4 没有给出任何默认路由,所以处理错误捕获,您可以重定向到默认路由

【讨论】:

    【解决方案2】:

    重定向将转到未定义组件的/details。你需要为/details定义一个组件。

    routes.ts

    export const ROUTES: Routes = [
      {
        path: '',
        redirectTo: '/details',
        pathMatch: 'full'
      },
      {
        path: 'details',
        component: DetailsComponent
      },
      {
        path: 'details/:id',
        component: ProjectDetailsComponent
      }
    ];
    

    details.component.ts

    @Component({
      selector: 'app-details',
      templateUrl: './details.component.html',
      styleUrls: ['./details.component.css']
    })
    export class DetailsComponent implements OnInit {
    
      constructor(private router: Router) { }
    
      ngOnInit() {
        this.router.navigate(['/details', 1]);
      }
    
    }
    

    【讨论】:

    • 感谢您的回答。我试过这种方式。但我总是得到DetailsComponent 而不是ProjectDetailsComponent,即使我通过了id。如果我为所有路由设置相同的组件,我总是按预期到达ProjectDetailsComponent,但我无法获得id,因为它是未定义的。
    • 您是否在 app.component.html 中添加了路由器插座?
    • 是的。我做到了。 &lt;div class="projects-panel"&gt; &lt;project-list [(selected)]="activeProject" [projects]="projects"&gt;&lt;/project-list&gt; &lt;router-outlet&gt;&lt;/router-outlet&gt; &lt;/div&gt;
    • 我相信,这证明插座有效。并且路由部分有效。但由于某种原因,它没有用参数确定路线。
    • 这对我有用,请尝试简化您的问题并查看路由的角度教程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 2019-10-14
    • 2020-11-17
    • 2020-07-27
    相关资源
    最近更新 更多