上Angular2路由每個主題有固定的導航欄角2選擇,而無需導航欄
<nav>
<a [routerLink]="['/component-one']">Component One</a>
<a [routerLink]="['/component-two']">Component Two</a>
</nav>
<router-outlet></router-outlet>
所以基本上的鏈接部件的一個上點擊時,該組件將在<nav></nav>
下呈現什麼我需要當你點擊組件一,全視圖(整頁)改變到組件1的視圖(因此沒有<nav></nav>
)
我試過puttin克<router-outlet>
在一個單獨的@Component
// view.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'view',
template:`<router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES]
})
export class ViewComponent {
}
然後
// home.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'home',
template: `
<nav>
<a [routerLink]="['/component-one']">Component One</a>
<a [routerLink]="['/component-two']">Component Two</a>
</nav>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
export class HomeComponent {
}
的應用程序組件看起來是這樣的:
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { HomeComponent } from './containers/home.component';
@Component({
moduleId: module.id,
selector: 'app-root',
template: `
<h1>{{title}}</h1>
<view></view>
<home></home>
`,
styleUrls: ['app.component.css'],
directives: [HomeComponent, ROUTER_DIRECTIVES]
})
export class AppComponent {
title = 'app works';
}
也試圖把<home>
內<view>
但沒有
始終得到同樣的錯誤:
Error: Cannot find primary outlet to load 'OneComponent'
任何人都可以幫助我呢? Thx!
編輯
// app.routes.ts
import { provideRouter, RouterConfig } from '@angular/router';
import { ViewComponent } from './containers/view.component';
import { HomeComponent } from './containers/home.component';
import { OneComponent } from './containers/one.component';
import { TwoComponent } from './containers/two.component';
const routes: RouterConfig = [
{
path: '',
redirectTo: '',
pathMatch: 'full'
},
{
path: '',
component: HomeComponent,
},
{
path: 'one',
component: OneComponent
},
{
path: 'two',
component: TwoComponent
}
];
export const appRouterProviders = [
provideRouter(routes)
];
你能粘貼你的路由器配置嗎? – MatWaligora
已添加到原始帖子 – Sfen