2017-03-01 49 views
2

我有一個應用程序,我無法加載一個簡單的組件。我覺得它很簡單,但我完全錯過了它。找不到主要插座加載

我的組件:

import { Component, OnInit } from '@angular/core'; 
import { Router } from '@angular/router'; 
import {PageStat} from './PageStat'; 
import {PageStatService} from './pageStatService'; 
@Component({ 
    moduleId: module.id, 
    selector: 'PageStats', 
    template: '<h2>You Made it!</h2>', 
    styleUrls: ['./basestyle.css'] 
}) 
export class PageStatsComponent implements OnInit{ 
    pagestats: PageStat[]; 
    selectedStat: PageStat; 
    constructor(
     private router: Router, 
     private pagestatservice: PageStatService){} 
    getPageStatList(){ 
    this.pagestatservice.getPageStats().then(pagestats => this.pagestats = pagestats) 

    } 
    ngOnInit(): void { 
    this.getPageStatList(); 
    } 
    onSelect(selectpagestat: PageStat){ 
     this.selectedStat = selectpagestat; 
    } 
    gotoDetail(): void { 
     this.router.navigate(['/detail', this.selectedStat.refid]); 
    } 
} 

,這是我的應用程序的路由:

import { NgModule }    from '@angular/core'; 
import { RouterModule, Routes } from '@angular/router'; 

import { PageStatsComponent }  from './PageStats.component'; 
import { PageStatDetailComponent } from './pagestat-detail.component'; 

const routes: Routes = [ 
    { path: '', redirectTo: '/pageStats', pathMatch: 'full' }, 
    { path: 'pagestats/detail/:id', component: PageStatDetailComponent }, 
    { path: 'pageStats',  component: PageStatsComponent } 
]; 

@NgModule({ 
    imports: [ RouterModule.forRoot(routes) ], 
    exports: [ RouterModule ] 
}) 
export class AppRoutingModule {} 

我曾嘗試剝離出部分並沒有什麼不同部位似乎改變結果。我試圖克隆這個「英雄之旅」例子。我看到很多引用「路由器插座」,但英雄應用程序的遊覽沒有一個,它的工作原理,所以我不是100%確定爲什麼或在哪裏我需要放置一個。

編輯:完整的錯誤:錯誤:無法找到主要出口到裝載「PageStatsComponent」

回答

3

你需要一個

<router-outlet></router-outlet> 

某處你的HTML,以便它知道在哪裏,以顯示該HTML當前路線。

在您的頂級組件做..

template: '<h2>here is the rest of my HTML for the top level component</h2><router-outlet></router-outlet>' 
+0

我知道了。當我嘗試路由器插座時,讓我感到困惑的是我誤解了去了哪裏。我沒有看到一個在我認爲它去的英雄應用程序的遊覽中,所以我認爲他們沒有一個。您對「頂級」組件的評論引導了我。PageStats組件不是我的頂級組件。謝謝! – facon12