在Angular 6中,可以使用三种不同的方法来声明方法类型。
function myMethod(arg1: string, arg2: number): void {
// 函数体
}
const myMethod = function(arg1: string, arg2: number): void {
// 函数体
};
const myMethod = (arg1: string, arg2: number): void => {
// 函数体
};
这些方法都可以在组件类中使用,例如:
@Component({
selector: 'my-component',
template: '{{result}}
'
})
export class MyComponent {
result: string;
// 使用函数声明
myMethod(arg1: string, arg2: number): void {
this.result = arg1 + arg2;
}
// 使用函数表达式
myMethod2 = function(arg1: string, arg2: number): void {
this.result = arg1 + arg2;
};
// 使用箭头函数
myMethod3 = (arg1: string, arg2: number): void => {
this.result = arg1 + arg2;
};
}
在上述示例中,myMethod
,myMethod2
和myMethod3
都是声明了方法类型的示例。你可以根据自己的需求选择其中一种方式来声明方法类型。