我想實現使用angular2-seed項目作爲基礎和引用angular2-login-seed的登錄功能。但我得到以下錯誤。 我看了一遍其他問題,詢問有關同樣的錯誤,但無法弄清楚我的代碼有什麼問題。請幫忙。未處理的承諾拒絕:無法找到主要插座來加載組件Angular2
Unhandled Promise rejection: Cannot find primary outlet to load 'HomeComponent' ; Zone: angular ; Task: Promise.then ; Value: Error: Cannot find primary outlet to load 'HomeComponent'
我的應用程序組件代碼:
@Component({
moduleId: module.id,
selector: 'sd-app',
viewProviders: [NameListService, HTTP_PROVIDERS],
templateUrl: 'app.component.html',
directives: [ROUTER_DIRECTIVES, NavbarComponent, ToolbarComponent]
})
export class AppComponent {
constructor() {
console.log('Environment config', Config);
}
}
app.component.html代碼:
<sd-navbar><router-outlet></router-outlet></sd-navbar>
應用路由配置:
const routes: RouterConfig = [
{
path: 'login',
component: LoginComponent,
canActivate: [UnauthenticatedGuard]
},
...HomeRoutes,
];
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes),UnauthenticatedGuard, HomeComponentGuard
];
HomeRoutes代碼:
export const HomeRoutes = [
{
path: '',
component: HomeComponent,
canActivate: [HomeComponentGuard],
children: [
{ path: '', component: HomeComponent },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent }
]
},
];
Home.Component代碼:
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { NameListService } from '../shared/index';
@Component({
moduleId: module.id,
selector: 'wrapper',
templateUrl: 'home.component.html',
styleUrls: ['home.component.css'],
directives: [ROUTER_DIRECTIVES]
})
export class HomeComponent {
newName: string;
constructor(public nameListService: NameListService) {
console.log("nameList: " + JSON.stringify(nameListService.get()));
}
addName(): boolean {
this.nameListService.add(this.newName);
this.newName = '';
return false;
}
}
您可以嘗試添加像'{path:'',component:HomeComponent,pathMatch:'full'},'0123'的路徑匹配:'full'', –
已嘗試使用完整路徑匹配,仍會引發相同的錯誤。 – chamalabey
Plunker https://angular.io/resources/live-examples/quickstart/ts/plnkr.html將有助於診斷。 –