angular2中的多个组件

0 angular2-template angular

我是角度2的新手,在多个组件中,我在一个文件夹文件中编写了两个组件,我已经在第一个文件中导入第二个类并给出了指令:class但它显示错误!这里是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 { }
Run Code Online (Sandbox Code Playgroud)

这里是第一个组件文件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 { }
Run Code Online (Sandbox Code Playgroud)

这里是第二个组件文件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 { }
Run Code Online (Sandbox Code Playgroud)

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

ran*_*al9 7

最近的角度@Component.directives已被弃用,所以你必须申报你mydetailsComponentAppModule

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 { }
Run Code Online (Sandbox Code Playgroud)

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 { }
Run Code Online (Sandbox Code Playgroud)