【问题标题】:Angular 2 Component Not Displaying in index.htmlAngular 2 组件未显示在 index.html 中
【发布时间】:2017-09-24 02:24:30
【问题描述】:

我对 Angular 2 很陌生,所以请多多包涵。 我试图让一个新组件出现在 index.html 页面中。 该文件集来自 GitHub 的基本快速入门文件。 我这样创建了一个新组件:

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

@Component({
    selector: 'app-user-item',
    template: `<h1>It works!</h1>`,
})
export class UserItemComponent {

}

我已经在 HTML 中声明了选择器标签:

<!DOCTYPE html>
<html>
  <head>
    <title>Angular QuickStart</title>
    <base href="/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

    <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>

    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>

    <script src="systemjs.config.js"></script>
    <script>
      System.import('main.js').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>

    <my-app>Loading AppComponent content here ...</my-app>
    <app-user-item>loading another component</app-user-item>
  </body>
</html>

我什至尝试将导入添加到 app.module.ts 的顶部,并将组件名称添加到 app.module.ts 中的声明数组。但还是一无所获。 我检查了我的文件结构,并且有一个 js 版本的 user-item.component.ts。但我看不到变化。

任何帮助将不胜感激。

干杯

【问题讨论】:

    标签: angular2-components


    【解决方案1】:

    在我的情况下,app.component.ts 文件中定义的 Selectorindex.html 不匹配

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。在您的 app.module.ts 文件中,确保在您的引导声明中包含您的组件。

      import { NgModule }      from '@angular/core';
      import { BrowserModule } from '@angular/platform-browser';
      
      import { AppComponent }  from './app.component';
      import { UserItemComponent } from './UserItem.component';
      
      @NgModule({
        declarations : [
          AppComponent,
          UserItemComponent
        ],
        imports:      [ 
          BrowserModule 
        ],
        bootstrap:    [ 
            AppComponent,
            UserItemComponent
        ],
        providers: [],
      })
      export class AppModule { }
      

      【讨论】:

        【解决方案3】:

        user-item.component.ts:

        import { Component } from '@angular/core';
        
        @Component ({
           selector: 'app-user-item',
           template: `<p>child item</p>`
        )}
        
        export class UserItemComponent {
          constructor(){ }
        }
        

        user.component.ts:

        import { Component } from '@angular/core';
        
        @Component({
          selector: 'app-user',
          template: ` <h2> My User Item </h2>
           <app-user-item></app-user-item>`
        })
        
        export class UserComponent { 
          constructor() {}
        }
        

        user.module.ts:

        import { NgModule } from '@angular/core';
        import { UserComponent } from './user-component';
        import { UserItemComponent } from './user-item-component';
        
        @NgModule({ 
          declarations: [
            UserComponent, 
            UserItemComponent
          ]
        })
        
        export class UserModule {}
        

        我真的鼓励你使用 angular-cli (https://github.com/angular/angular-cli)。并在角度页面上做教程。 但是,对于您的用例,上面的代码应该足够并且可以工作。 通常你在与父组件相同的模块中声明你的子组件。 该模块由app.module(根模块)导入

        看Angular的Master-Detail组件教程 https://angular.io/docs/ts/latest/tutorial/toh-pt2.html

        【讨论】:

        • 干杯。我实际上创建了一种所有 html 文件都呈现在其中的父模板。但是感谢您的回答。
        猜你喜欢
        • 2017-01-03
        • 1970-01-01
        • 2016-07-13
        • 1970-01-01
        • 2016-10-06
        • 1970-01-01
        • 1970-01-01
        • 2019-05-02
        • 2016-07-09
        相关资源
        最近更新 更多