Angular2 路由的每个主题都有一个固定的导航栏
<nav>
<a [routerLink]="['/component-one']">Component One</a>
<a [routerLink]="['/component-two']">Component Two</a>
</nav>
<router-outlet></router-outlet>
所以基本上当点击组件一的链接时,这个组件将呈现在下面<nav></nav>
我需要的是当您单击组件一时,完整视图(整页)更改为组件一的视图(因此没有<nav></nav>
)
我试着把它放在<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 {
}
App 组件如下所示:
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>
,但什么也没有
总是得到同样的错误:
错误:找不到加载“OneComponent”的主要出口
任何人都可以帮助我吗?谢谢!
编辑
// 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)
];