在字符串插值中无法识别 Typescript 中的箭头函数 (Angular)
Arrow function in Typescript not recognized in string interpolation (Angular)
在TypeScript中使用箭头函数进行字符串插值时,浏览器中没有输出预期的内容"Hello",也没有报错。但它会在使用正常函数语法时显示。为什么会这样?
app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
getStr: () => 'Hello';
}
app.component.html:
<p>{{ getStr() }}</p>
更改为 getStr = () => 'Hello';
,因为您需要初始化 getStr。
如果你也想添加打字,你可以这样做:
getStr: () => string = () => "Hello";
在TypeScript中使用箭头函数进行字符串插值时,浏览器中没有输出预期的内容"Hello",也没有报错。但它会在使用正常函数语法时显示。为什么会这样?
app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
getStr: () => 'Hello';
}
app.component.html:
<p>{{ getStr() }}</p>
更改为 getStr = () => 'Hello';
,因为您需要初始化 getStr。
如果你也想添加打字,你可以这样做:
getStr: () => string = () => "Hello";