我正在尝试按照本教程http://onehungrymind.com/named-router-outlets-in-angular-2/,但我收到错误。
EXCEPTION:未捕获(承诺):错误:无法匹配任何路由。网址 细分:'clients'错误:无法匹配任何路由。网址细分: '客户端'
这是我的Router module
import {NgModule} from "@angular/core";
import {Routes, RouterModule} from "@angular/router";
import {AppComponent} from "./app.component";
import {ClientComponent} from "./clients/client.component";
import {ClientDetailsComponent} from "./clients/client-details/client-details.component";
import {ClientListComponent} from "./clients/client-list/client-list.component";
const routes: Routes = [
{path: '', component: AppComponent},
{path: 'clients', component: ClientComponent, children: [
{path: 'list', component: ClientListComponent, outlet: 'client-list'},
{path: ':id', component: ClientDetailsComponent, outlet: 'client-details'}
]
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: []
})
export class RoutingModule {
}
App.component
export class AppComponent {
clientsLoaded = false;
constructor(private clientService: ClientService,
private router: Router) {
}
loadClientsComponent() {
this.router.navigate(['/clients', {outlets: {'client-list': ['clients'], 'client-details': ['none'], 'client-accounts-info': ['none']}}]);
this.clientsLoaded = true;
}
}
Client.component
import {Component} from "@angular/core";
import {ClientService} from "../services/client.service";
import {Router} from "@angular/router";
import {Client} from "./client-details/model/client.model";
@Component({
selector: 'clients',
templateUrl: 'app/clients/client.component.html'
})
export class ClientComponent {
clients: Array<Client> = [];
constructor(private clientService: ClientService,
private router: Router) {
}
ngOnInit() {
this.getClients().subscribe((clients) => {
this.clients = clients;
});
}
getClients() {
return this.clientService.getClients();
}
}
有什么不对?我将不胜感激任何帮助
更新
感谢您的建议,我尝试了类似的东西
const routes: Routes = [
{path: '', component: AppComponent, pathMatch: 'full'},
{path: 'clients', component: ClientComponent, children: [
{path: '', component: ClientComponent},
{path: 'list', component: ClientListComponent, outlet: 'client-list'},
{path: ':id', component: ClientDetailsComponent, outlet: 'client-details'}
]
},
];
但它仍然不起作用。
也许我做错了,但我的ClientComponent
<div id="sidebar" class="col-sm-3 client-sidebar">
<h2 class="sidebar__header">
Client list
</h2>
<div class="sidebar__list">
<router-outlet name="client-list"></router-outlet>
</div>
</div>
<div id="client-info" class="col-sm-4 client-info">
<div class="client-info__section client-info__section--general">
<router-outlet name="client-details"></router-outlet>
</div>
</div>
所以我希望我的客户端组件保留所有与客户端相关的组件。如果用户点击了例如按钮,则会加载客户端列表。
答案 0 :(得分:9)
添加pathMatch: 'full'
{path: '', component: AppComponent, pathMatch: 'full'},
到路径为空''
且没有子路径的路由。
答案 1 :(得分:2)
我正在将Angular 2/4集成到现有的MVC应用程序中,我也遇到了这个问题。
使用您的示例,如果我在路由配置中使用path: 'Clients'
而不是path: 'clients'
并尝试转到http://localhost/clients
等网址,我会收到此错误。
路径路径名称区分大小写(至今为止)。他们不会与你的URL中的内容相匹配,并且通过典型的MVC控制器/动作命名,它们是&#34; InitialCaps风格。&#34;
答案 2 :(得分:0)
在 app.module.ts 中检查导入会话,看您没有错过路由:
imports: [
BrowserModule,
APP_ROUTES,
AppRoutingModule
],
答案 3 :(得分:0)
我遇到了同样的问题,我的道路是正确的。 最后,清洁ClientApp \ dist文件夹解决了该问题。