Angular 将app.component.ts文件中的iLike元素绑定到like.component.ts时无法绑定错误
我收到这个错误: 异常:模板分析错误: 无法绑定到“ilike”,因为它不是已知的本机属性(“ ][ilike]=“tweet.ilike”> "): AppComponent@1:42Angular 将app.component.ts文件中的iLike元素绑定到like.component.ts时无法绑定错误,angular,Angular,我收到这个错误: 异常:模板分析错误: 无法绑定到“ilike”,因为它不是已知的本机属性(“ ][ilike]=“tweet.ilike”> "): AppComponent@1:42 import {Component, Input} from 'angular2/core'; @Component({ selector: 'like', template: ` <i class="glyphicon glyphicon-heart" [class.highlighte
import {Component, Input} from 'angular2/core';
@Component({
selector: 'like',
template: `
<i
class="glyphicon glyphicon-heart"
[class.highlighted]="iLike"
(click)="onClick()">
</i>
<span>{{ totalLikes }}</span>
`,
styles: [`
.glyphicon-heart {
color: #ccc;
cursor: pointer;
}
.highlighted {
color: deeppink;
}
`]
})
export class LikeComponent {
@Input() totalLikes = 0;
@Input() iLike = false;
onClick(){
this.iLike = !this.iLike;
this.totalLikes += this.iLike ? 1 : -1;
}
}
从'angular2/core'导入{组件,输入};
@组成部分({
选择器:“像”,
模板:`
{{totalikes}}
`,
风格:[`
.Glypicon心脏{
颜色:#ccc;
光标:指针;
}
.突出显示{
颜色:深粉色;
}
`]
})
导出类LikeComponent{
@Input()totalikes=0;
@Input()iLike=false;
onClick(){
this.iLike=!this.iLike;
this.totalikes+=this.iLike?1:-1;
}
}
上面的代码是我的like.component.ts文件,我在其中有一颗心,当我在上面单击时,它会变成紫色,totalikes值增加一,当用户再次单击时,它会转换回灰色,值减少一
import {Component} from 'angular2/core'; // this is component decorator
import {LikeComponent} from './like.component'
@Component({
selector: 'my-app',
template: `
<like [totalLikes]="tweet.totalLikes" [ilike]="tweet.iLike">
</like>
`,
directives: [LikeComponent]
})
export class AppComponent {
tweet={
totalLikes: 10,
iLike: false
}
}
从'angular2/core'导入{Component};//这是组件装饰器
从“./like.component”导入{LikeComponent}
@组成部分({
选择器:“我的应用程序”,
模板:`
`,
指令:[LikeComponent]
})
导出类AppComponent{
推特={
总计:10,
我喜欢:错
}
}
上面是我的like.component.ts
有两个小问题 (i) 您尚未在app.module.ts中导入类似组件的
(ii)iLike中存在打字错误,应该是
template: `
<like [totalLikes]="tweet.totalLikes" [iLike]="tweet.iLike">
</like>
`,
模板:`
`,