Multiple Components in angular2

笑着哭i 提交于 2019-12-10 11:52:13

问题


I'm new to angular 2 , In multiple components i have written a two components in a single folder file i have import the 2nd class in first file and given directive: class but its showing error! Here the app.module.ts file

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 { }

Here the first component file 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 { }

Here second Component file 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 { }

Please tell us how to use and display the multiple components!


回答1:


in latest angular @Component.directives are deprecated so you have to declare your mydetailsComponent in AppModule

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 { }


来源:https://stackoverflow.com/questions/40678088/multiple-components-in-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!