age = 22;
fruit = 'apple'
}
上面的写法其实只是一种简写的形式,实际上完整的写法应该是这样的:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
export class AppComponent {
//username = 'dreamapple';
//age = 22;
//fruit = 'apple'
username: string;
age: number;
fruit: string;
constructor() {
this.username = 'dreamapple';
this.age = 22;
this.fruit = 'apple';
}
}
然后我们要修改我们的模板了,因为我们在模板中要使用较多的HTML,所以我们要使用反引号来包裹住我们的HTML片段;我们的装饰器函数如下所示:
?
1
2
3
4
5
6
7
8
@Component({
selector: 'my-app',
template:`
My name is: {{username}}