Angularjs2-一個頁面顯示多個組件


bootstrap - 標識出應用的主視圖(被稱為 根組件 ),它是所有其它視圖的宿主。只有 根模塊 才能設置 bootstrap 屬性。

bootstrap - the main application view, called the root component, that hosts all other app views. Only the root module should set this bootstrap property.

深刻理解,其他視圖宿主!!

//app.component.ts
@Component({
  selector: 'my-app',
  template: `
    <h1>helloApp</h1>
  `
})

//header.component.ts
@Component({
  selector: 'my-header',
  template: `
    <h1>helloHeader</h1>
  `
})
//app.module.ts
@NgModule({
  ...
  bootstrap:    [ AppComponent]
  ...
})
index.html
<body>
    <my-header></my-header>
    <my-app>Loading...</my-app>
</body>

瀏覽網頁,會發現,只有my-app添加上了,my-header去哪里了呢?說好的組件化呢?r u kidding me?

解決方案一:

將my-header從index.html中移動到app.component.ts中,如下

//app.component.ts
@Component({
  selector: 'my-app',
  template: `
    <my-header></my-header>
    <h1>helloApp</h1>
  `
})

為什么這樣就能加上了呢?思考中。。。。

bootstrap 視圖宿主,難道是。。。見解決方案二

解決方案二:

直接修改app.module.ts

//app.module.ts
@NgModule({
  ...
  bootstrap:    [ AppComponent,HeaderComponent]
  ...
})

參考資料:stackoverflow


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM