如何在Angular2中渲染具有多个插座的局部视图

时间:2017-03-29 10:04:07

标签: angular angular2-routing angular2-template angular2-directives

我是Angular2的初学者,想要用路由概念创建简单的CRUD操作。我正在使用Visual Studio 2015,所以所有配置都已经可以使用了。我想在现有视图中打开一个视图,如下面的屏幕,

Expected result

获取,后视图在下方区域(部分内容区域)渲染,

我的模块,

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { UniversalModule } from 'angular2-universal';
import { AppComponent } from './components/app/app.component'
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { CounterComponent } from './components/counter/counter.component';
import { FetchDataGetComponent } from './components/fetchdata/fetchdata.get.component'
import { FetchDataPostComponent } from './components/fetchdata/fetchdata.post.component'

@NgModule({
    bootstrap: [AppComponent],
    declarations: [
        AppComponent,
        NavMenuComponent,
        CounterComponent,
        FetchDataComponent,
        HomeComponent,
        FetchDataGetComponent,
        FetchDataPostComponent
    ],
    imports: [
        UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
        RouterModule.forRoot([
            { path: '', redirectTo: 'home', pathMatch: 'full' },
            { path: 'home', component: HomeComponent },
            { path: 'counter', component: CounterComponent },
            { path: 'data', component: FetchDataComponent },
            { path: 'data/get', component: FetchDataGetComponent, outlet: 'datachild' },
            { path: 'data/post', component: FetchDataPostComponent, outlet:'datachild' },
            { path: '**', redirectTo: 'home' }
        ])
    ]
})
export class AppModule {
}

App.component.html,

<div class='container-fluid'>
    <div class='row'>
        <div class='col-sm-3'>
            <nav-menu></nav-menu>
        </div>
        <div class='col-sm-9 body-content'>
            <router-outlet></router-outlet>
            <router-outlet name='datachild'></router-outlet>
        </div>
    </div>
</div>

fetchdata.component.html

<div class='navbar-collapse collapse'>
    <ul class='nav navbar-nav'>
        <li [routerLinkActive]="['link-active']">
            <a [routerLink]="['/data/get']">
                <span class='glyphicon glyphicon-home'></span> GET
            </a>
        </li>
        <li [routerLinkActive]="['link-active']">
            <a [routerLink]="['/data/post']">
                <span class='glyphicon glyphicon-education'></span> POST
            </a>
        </li>
        <li [routerLinkActive]="['link-active']">
            <a [routerLink]="['/databyid']">
                <span class='glyphicon glyphicon-th-list'></span> Fetch data by id
            </a>
        </li>
    </ul>
</div>
<div>
    <h1>Partial content area</h1>
</div>

我正在使用插座功能来做我的事情,但没有运气,其他链接(Home,Counter ...)正在工作,但GET,POST无法正常工作,我需要在哪里进行更改才能使其正常工作? / p>

1 个答案:

答案 0 :(得分:0)

请参阅routerlink has&#39; / data / get&#39;而在路由文件中,它的数据/获取&#39;。 从&#39; / data / get&#39;

中删除第一个斜杠