在Angular 6中,在新类的构造函数中提供函数的解决方法如下:
首先,创建一个新的类,然后在构造函数中引入所需的函数。以下是一个示例代码:
import { Component } from '@angular/core';
class MyFunction {
myFunction() {
// 执行一些操作
console.log('执行了我的函数');
}
}
@Component({
selector: 'app-my-component',
template: ''
})
export class MyComponent {
myFunction: Function;
constructor() {
// 在构造函数中提供函数
const myFunction = new MyFunction();
this.myFunction = myFunction.myFunction;
}
callMyFunction() {
// 调用函数
this.myFunction();
}
}
在上述代码中,我们创建了一个名为MyFunction
的新类,并在其中定义了一个myFunction
函数。然后,在MyComponent
组件的构造函数中,我们实例化了MyFunction
类,并将myFunction
函数赋值给了myFunction
属性。
在组件模板中,我们使用一个按钮来调用callMyFunction
方法。在该方法中,我们调用了myFunction
属性,从而调用了MyFunction
类中的myFunction
函数。
这样,当我们点击按钮时,将会执行MyFunction
类中的myFunction
函数,并在控制台中输出相应的信息。