Angular 4 - 将数据传递到组件中
Angular 4 - pass data into components
我真的很困惑,我试图在Angular 4中从一个组件向另一个组件注入数据,看起来数据通过了但它赢了'显示出来,我直接上示例代码:
board.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'game-board',
template: `<card [init]="name"></card>`,
})
export class BoardComponent {
name: string = 'this is the card name';
}
Card.component.ts:
import { Component, Input } from '@angular/core';
@Component({
selector: 'card',
template: `<div class="card-container">
CARD - {{strength}}
</div>`,
})
export class CardComponent {
@Input('init') strength: string;
}
我希望结果是 "CARD - this is the card name"
但它只是 "CARD - ".
这是元素的样子(chrome 开发工具,忽略 'CARD -' 和 '' 之间的差距):
所以我看到注入有效并且 "this is the card name"
通过了,但由于某种原因 strength
未定义。
我做错了什么?
确保在 ngModule 定义的 bootstrap 属性中定义了主要组件,并且两个组件都定义为声明。
应该看起来像这样:
@NgModule({
imports: [ BrowserModule ],
declarations: [ BoardComponent, CardComponent ],
bootstrap: [ BoardComponent ]
})
只有主要组件应该在 bootstrap 数组中定义,并且都在声明数组中定义。
我真的很困惑,我试图在Angular 4中从一个组件向另一个组件注入数据,看起来数据通过了但它赢了'显示出来,我直接上示例代码:
board.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'game-board',
template: `<card [init]="name"></card>`,
})
export class BoardComponent {
name: string = 'this is the card name';
}
Card.component.ts:
import { Component, Input } from '@angular/core';
@Component({
selector: 'card',
template: `<div class="card-container">
CARD - {{strength}}
</div>`,
})
export class CardComponent {
@Input('init') strength: string;
}
我希望结果是 "CARD - this is the card name" 但它只是 "CARD - ".
这是元素的样子(chrome 开发工具,忽略 'CARD -' 和 '' 之间的差距):
所以我看到注入有效并且 "this is the card name"
通过了,但由于某种原因 strength
未定义。
我做错了什么?
确保在 ngModule 定义的 bootstrap 属性中定义了主要组件,并且两个组件都定义为声明。 应该看起来像这样:
@NgModule({
imports: [ BrowserModule ],
declarations: [ BoardComponent, CardComponent ],
bootstrap: [ BoardComponent ]
})
只有主要组件应该在 bootstrap 数组中定义,并且都在声明数组中定义。