【发布时间】:2018-01-29 14:51:10
【问题描述】:
我收到如下错误:
[ts] 预期声明或声明
当我将元素从child 传递到parent 时,即是在练习双向沟通。我没有得到什么是错误
//child
import { Component, Input, EventEmitter, Output } from '@angular/core';
//import { Hero } from './app.hero';
@Component({
selector: 'bro-root',
template:`
<div>
<h1>name:</h1>
<h2>{{fname}}</h2>
<h2>{{sname}}</h2>
<h2 style="background-color:red;width:80px;">{{bruz}}</h2>
<h2 style="background-color:red;width:80px;">{{no}}</h2>
</div>
`,
})
export class ChildComponent {
fname:string = "tom";
sname:string = "jerry";
gender:string = "male";
age:number = 20;
@Input()
bruz:string;
@Input()
no:number;
@Output()
change:EventEmitter<number> = new EventEmitter<number>();
this.change.emit(11);
}
//parent
import { Component, Input } from '@angular/core';
import { ChildComponent } from './app.childcomponent';
//import { Hero } from './app.Hero';
@Component({
selector: 'app-root',
template: `
<bro-root[no]="count" (change)="updateFromChild($event)"></bro-root>
`,
})
export class AppComponent {
title = 'app works!';
count:number = 10;
updateFromChild($event) {
this.count++;
}
}
【问题讨论】:
-
任何好的(trans/)编译器都会给你错误所在的行。此外,任何好的 IDE 都会用红色强调它。你在写什么,纸?
-
那么您是否设法修复了您的代码?
标签: angular angular2-components