【问题标题】:Multiple Components in angular2angular2中的多个组件
【发布时间】:2016-11-18 13:16:07
【问题描述】:

我是 angular 2 的新手,在多个组件中,我在一个文件夹文件中编写了两个组件,我在第一个文件中导入了第二个类并给出了指令:类,但它显示错误! 这里是 app.module.ts 文件

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

这里是第一个组件文件app.component.ts

import { Component } from '@angular/core';
import { mydetailsComponent } from './app.details';

@Component({
   selector: 'my-app',
   template: `<h1>Welcome to this Application...</h1>
   <p>We have the App details here:</p>
   <mydetails></mydetails>
   `
  directives: [ mydetailsComponent ] })
export class myAppComponent { }

这里是第二个组件文件app.details.ts

import { Component } from '@angular/core';
@Component({
  selector: 'mydetails',
  template: `<ul>
             <li>Settings</li>
             <li>Profile</li>
             <li>Games</li>
             <li>Gallery</li>
            `
})
export class mydetailsComponent { }

请告诉我们如何使用和显示多个组件!

【问题讨论】:

    标签: angular angular2-template


    【解决方案1】:

    不推荐使用最新的角度@Component.directives,因此您必须在AppModule 中声明您的mydetailsComponent

    app.module.ts

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

    app.component.ts

    import { Component } from '@angular/core';
    import { mydetailsComponent } from './app.details';
    
    @Component({
      selector: 'my-app',
      template: `<h1>Welcome to this Application...</h1>
        <p>We have the App details here:</p>
        <mydetails></mydetails>
      `
    })
    export class myAppComponent { }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-04
      • 2016-08-30
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      相关资源
      最近更新 更多