无法匹配任何路线。网址细分

时间:2017-09-27 06:10:14

标签: angular angular2-routing

我在设备组件中有以下链接,我希望它将我路由到命令设备组件

<a [routerLink]="['/devicescommands',device.Id]" class="col-1-4" data-toggle="tooltip" data-placement="top" title="کد های بازگشت">
                            <span class="glyphicon glyphicon-book" aria-hidden="true"></span>
                        </a>

这是我的路由模块:

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

import { DeviceComponent } from './device/device.component';
import { CommandComponent } from './device/command/command.component';
import { DeviceConfigurationComponent } from './device/configuration/device-
configuration.component';
import { ModulesComponent } from './modules/modules.component';
import { ModuleConfigurationComponent } from 
'./modules/configuration/module-configuration.component';

const routes: Routes = [
{ path: '', redirectTo: '/modules' },

{ path: 'modules', component: ModulesComponent },
{ path: 'moduleconfiguration/:id', component: ModuleConfigurationComponent 
},

{ path: 'devices', component: DeviceComponent},
{ path: 'devicescommands/:id', component: CommandComponent },
{ path: 'deviceconfiguration/:id', component: DeviceConfigurationComponent }
];

@NgModule({
imports: [
    RouterModule.forRoot(
        routes,
        { enableTracing: true }
    )
],
exports: [RouterModule]

}) 导出类AppRoutingModule {}

这是app模块:

import { NgModule, APP_INITIALIZER } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { HashLocationStrategy, LocationStrategy } from '@angular/common';

import { AppRoutingModule } from './app-routing.module';
import { CatalogServerService } from './share/catalog-server-service/catalog-server-service'
import { StatusPipe } from './share/pipes/status.pipe';
import { ProfileConfigPipe } from './share/pipes/profile-config.pipe';
import { PersianDatePipe } from './share/pipes/persian-date.pipe';
import { SuccessFaildPipe } from './share/pipes/success-faild.pipe';

import { AppComponent } from './app.component';
import { DeviceComponent } from './device/device.component';
import { DeviceService } from './device/services/device.service';
import { DeviceConfigurationComponent } from './device/configuration/device-
 configuration.component';
import { CommandComponent } from './device/command/command.component';

import { ModulesComponent } from './modules/modules.component';
import { ModulesService } from './modules/services/modules.service';
import { ModuleConfigurationComponent } from 
'./modules/configuration/module-configuration.component';

import { DynamicFormComponent } from './share/dynamic-from/dynamic-
 form.component';
import { DynamicFormControlComponent } from './share/dynamic-from/dynamic-
 form-control.component';

import { SignalRService } from './share/services/signalr-r-service';

@NgModule({
imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    ReactiveFormsModule,
    AppRoutingModule
],
declarations: [
    DeviceComponent,
    DeviceConfigurationComponent,
    CommandComponent,
    ModulesComponent,
    ModuleConfigurationComponent,
    StatusPipe,
    PersianDatePipe,
    ProfileConfigPipe,
    SuccessFaildPipe,
    DynamicFormComponent,
    DynamicFormControlComponent,
    AppComponent,
],
providers: [
    CatalogServerService,
    {
        provide: APP_INITIALIZER,
        useFactory: (configService: CatalogServerService) => { return () => configService.loadConfig(); },
        deps: [CatalogServerService],
        multi: true
    },
    ModulesService,
    DeviceService,
    SignalRService,
    { provide: LocationStrategy, useClass: HashLocationStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule { }

当我点击devicescommands链接时,我希望它路由到CommandComponent,但我在控制台中收到此错误:

errors.ts:42 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'devicescommands/96c1f24d-6673-4df0-9704-e44842821605'
Error: Cannot match any routes. URL Segment: 'devicescommands/96c1f24d-6673-4df0-9704-e44842821605'
    at ApplyRedirects.noMatchError (apply_redirects.ts:160)
    at CatchSubscriber.eval [as selector] (apply_redirects.ts:142)
    at CatchSubscriber.error (catch.ts:55)
    at MapSubscriber.Subscriber._error (Subscriber.ts:139)
    at MapSubscriber.Subscriber.error (Subscriber.ts:109)
    at MapSubscriber.Subscriber._error (Subscriber.ts:139)
    at MapSubscriber.Subscriber.error (Subscriber.ts:109)
    at MapSubscriber.Subscriber._error (Subscriber.ts:139)
    at MapSubscriber.Subscriber.error (Subscriber.ts:109)
    at LastSubscriber.Subscriber._error (Subscriber.ts:139)
    at ApplyRedirects.noMatchError (apply_redirects.ts:160)
    at CatchSubscriber.eval [as selector] (apply_redirects.ts:142)
    at CatchSubscriber.error (catch.ts:55)
    at MapSubscriber.Subscriber._error (Subscriber.ts:139)
    at MapSubscriber.Subscriber.error (Subscriber.ts:109)
    at MapSubscriber.Subscriber._error (Subscriber.ts:139)
    at MapSubscriber.Subscriber.error (Subscriber.ts:109)
    at MapSubscriber.Subscriber._error (Subscriber.ts:139)
    at MapSubscriber.Subscriber.error (Subscriber.ts:109)
    at LastSubscriber.Subscriber._error (Subscriber.ts:139)
    at resolvePromise (zone.js:769)
    at resolvePromise (zone.js:740)
    at zone.js:817
    at ZoneDelegate.invokeTask (zone.js:424)
    at Object.onInvokeTask (ng_zone.ts:253)
    at ZoneDelegate.invokeTask (zone.js:423)
    at Zone.runTask (zone.js:191)
    at drainMicroTaskQueue (zone.js:584)
    at HTMLAnchorElement.ZoneTask.invoke (zone.js:490)

devicescommands链接在DeviceComponent中。

2 个答案:

答案 0 :(得分:2)

我认为您的devices路径会干扰您的devicescommands路径,因为两者都以&#39;设备开头。如果您按这样切换两个顺序:

{ path: 'devicescommands/:id', component: CommandComponent },
{ path: 'devices', component: DeviceComponent},

然后它应该工作。

答案 1 :(得分:0)

您的CommandComponent应该从ActivatedRoute导入@angular/routes,并且必须声明id属性,因为您使用的标识符似乎已在您的路径中使用path: 'devicescommands/:id'

您的CommandComponent应如下所示:

import { ActivatedRoute } from '@angular/router';

@Component({
  .......................................
})

export class CommandComponent implements OnInit {

  id:number;

  constructor(public _route:ActivatedRoute) { }

  ngOnInit() {
    this.id = this._route.snapshot.params["id"];
  }

}

您的HTML模板可以使用它:

<p>
  CommandComponent recieved the id: {{id}}
</p>