Angular2 - 不呈现多个组件

时间:2017-10-10 10:54:02

标签: angular templates components render

我有一个简单的应用程序 - 3个组件,一页。问题是第一个组件呈现,但我创建的两个自定义组件 - 他们没有。我已经向他们添加了未呈现的模板数据,index.html将仅加载预览文本,但不加载组件数据。我真的很欣赏一双额外的眼睛,我看不出我忽略了什么,解释会有用。我还是新手。谢谢

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import { HeadComponent }  from './head.component';
import { FooterComponent }  from './footer.component';


@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent, HeadComponent, FooterComponent ],
  bootstrap:    [ AppComponent ]
})

export class AppModule { }

app.component.ts

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

@Component({
  selector: 'my-app',
  template: `
  <h1>This is my-app</h1>
  <p><strong> {{id}}</strong></p>
  <p><strong> {{title}}</strong></p>
  <p><strong> {{description}}</strong></p>
  `,
})

export class AppComponent implements OnInit{

  id:string;
  title:string;
  description:string;


  constructor(){
    console.log('constructor called.')
  }

  // lifecycle hook
  ngOnInit(){
    this.id = '1';
    this.title = 'Full Stack - Angular 2 Java';
    this.description = 'lorum isum is some text..';
  }

 }


  interface jobInfo {
    id:string;
    title:string;
    description:string;
  }

head.component.ts

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

@Component({
  selector: 'my-head',
  template: `
  <div>
    <h1>This is my-header</h1>
    <label>testtest</label>
  </div>
  `,

})

export class HeadComponent implements OnInit{

  id:string;
  title:string;
  description:string;


  constructor(){
    console.log('constructor called.')
  }

  // lifecycle hook
  ngOnInit(){
    this.id = '1';
    this.title = 'Full Stack - Angular 2 Java';
    this.description = 'lorum isum is some text..';
  }

 }


  interface jobData {
    id:string;
    title:string;
    description:string;
  }

footer.component.ts

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

@Component({
  selector: 'my-foot',
  template: `
   <h1>This is my-foot</h1>
   <label>testtest</label>
  `,
})

export class FooterComponent implements OnInit{




  constructor(){
    console.log('constructor called.')
  }

  // lifecycle hook
  ngOnInit(){

  }

 }


  interface jobData {
    id:string;
    title:string;
    description:string;
  }

1 个答案:

答案 0 :(得分:-1)

AppComponent

模板中使用您的组件
@Component({
  selector: 'my-app',
  template: `
  <my-head></my-head>
  <h1>This is my-app</h1>
  <p><strong> {{id}}</strong></p>
  <p><strong> {{title}}</strong></p>
  <p><strong> {{description}}</strong></p>
  <my-foot></my-foot>
  `,
})