Next.js 会自动检测 jsconfig.json
或 tsconfig.json
中的experimentalDecorators
。tsconfig.json
{
"compilerOptions": {
//...
"experimentalDecorators": true
}
}
然后重启服务 否则装饰器无法识别
不声明的话vscode 执行 ts检测 也会报错作为表达式调用时,无法解析类修饰器的签名。
支持的四种装饰器
declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;
类装饰器
function cameraDecorator<T extends { new (...args: any[]): {} }>(
constructor: T
) {
return class extends constructor {
camera = ThreeHelper.instance.camera;
cameraWrapper = ThreeHelper.instance.camera.parent;
};
}
@cameraDecorator<typeof Asteroids>
class Asteroids {
private camera!: THREE.PerspectiveCamera;
private cameraWrapper!: THREE.Object3D;
}
也可以这样写 下方写法可以将属性写在当前class上文章来源:https://www.toymoban.com/news/detail-631641.html
export function UnrealBloomEffect<T extends { new (...args: any[]): {} }>(
constructor: T
) {
return class extends constructor {
name: string;
constructor(...rest: any[]) {
super(...rest);
this.name = "deo";
}
render() {
throw new Error("先执行initEffect");
}
}
}
如果是写在constructor.property上则可能写到Object上污染object基类文章来源地址https://www.toymoban.com/news/detail-631641.html
到了这里,关于Next.js使用装饰器decorator 解决[作为表达式调用时,无法解析类修饰器的签名。]的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!