在angular 2 asp.net核心中单独布局导航

时间:2017-01-08 05:41:12

标签: angular angular-routing

我的主要布局有标题正文和页脚。在某些页面中,我不想仅仅显示正文和页脚的标题。

我已经完成了this answer我试图实施但我无法弄明白该怎么做。

这是我的代码:

app.component.ts

import { Component } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
@Component({
    selector: 'main-app',
    template: `<router-outlet name="header"></router-outlet>
                <router-outlet name="navbar"></router-outlet>
                <router-outlet></router-outlet>
                <router-outlet name="footer"></router-outlet>`
})
export class AppComponent { }

Footer.component.ts

import { Component } from '@angular/core';

@Component({
    selector: 'Footer-app',
    templateUrl: '<p>Copy rights emakitri 2017</p>'
})
export class FooterComponent {
    constructor() {
        console.log("test");
    }
}

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { HttpModule, JsonpModule } from '@angular/http';
import { ModuleWithProviders } from '@angular/core';
import { AppComponent} from "./app.component";
import { EqualValidator } from "./Validation/equal.validator.directive";
import { LoginComponent } from "./Components/login.Component";
import { HomeComponent } from "./Components/home.component";
import { DashBoardComponent } from "./Components/dashBoard.Component";
import { FooterComponent } from "./Components/footer.Component";

const appRoutes: Routes = [
    { path: '', redirectTo: 'Home/Index', pathMatch: 'full' },
    { path: 'Account/Login', component: LoginComponent },
    { path: 'Home/Index', component: HomeComponent,children: [
        {path: '', component: FooterComponent , outlet: 'footer'}
        ] },
    { path: 'DashBoard/Index', component: DashBoardComponent}
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);


@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule, routing],
    declarations: [AppComponent, LoginComponent, HomeComponent, DashBoardComponent, EqualValidator,FooterComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

当我导航到Home / Index时,页脚容器不会出现。我究竟做错了什么?我是否必须为childr路由导入一些内容?

2 个答案:

答案 0 :(得分:0)

'footer'是'Home / Index'的孩子,

所以你需要:

<router-outlet name="footer"></router-outlet>

在HomeComponent模板中

您尝试做的示例:

https://plnkr.co/edit/sgGDpti43GPM5cHntPpu?p=preview

祝你好运! :)

答案 1 :(得分:0)

这是工作模块

<强> App.Module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { Routes,RouterModule} from '@angular/router';
import { HttpModule, JsonpModule } from '@angular/http';
import { ModuleWithProviders } from '@angular/core';
import { AppComponent} from "./app.component";
import { EqualValidator } from "./Validation/equal.validator.directive";
import { LoginComponent } from "./Components/login.Component";
import { HomeComponent } from "./Components/home.component";
import { DashBoardComponent } from "./Components/dashBoard.Component";
import { FooterComponent } from "./Components/footer.Component";
import { NavComponent } from "./Components/nav.Component";


const appRoutes: Routes = [
    { path: '', redirectTo: 'Home/Index', pathMatch: 'full' },
    { path: 'DashBoard/Index', component: DashBoardComponent},
    { path: 'Account/Login', children: [
            {path: '', component: NavComponent, outlet: 'navbar'},
            {path: '', component: LoginComponent},
            {path: '', component: FooterComponent, outlet: 'footer'}
            ]},
    { path: 'Home/Index', children: [
            {path: '', component: NavComponent, outlet: 'navbar'},
            {path: '', component: HomeComponent},
            {path: '', component: FooterComponent, outlet: 'footer'}
            ]}
];



export const routing = RouterModule.forRoot(appRoutes);

@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule,routing],
    declarations: [AppComponent, LoginComponent, HomeComponent, DashBoardComponent, EqualValidator,FooterComponent,NavComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

<强> App.Component.ts

import { Component } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
@Component({
    selector: 'main-app',
    template: `<router-outlet name="navbar"></router-outlet>
                <div class="container">
                <router-outlet></router-outlet>
                </div>
                <router-outlet name="footer"></router-outlet>`
})
export class AppComponent { }