Next.js 會自動檢測 jsconfig.json
或 tsconfig.json
中的experimentalDecorators
。tsconfig.json
{
"compilerOptions": {
//...
"experimentalDecorators": true
}
}
然后重啟服務(wù) 否則裝飾器無法識別
不聲明的話vscode 執(zhí)行 ts檢測 也會報(bào)錯作為表達(dá)式調(diào)用時,無法解析類修飾器的簽名。
支持的四種裝飾器
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;
}
也可以這樣寫 下方寫法可以將屬性寫在當(dāng)前class上文章來源:http://www.zghlxwxcb.cn/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("先執(zhí)行initEffect");
}
}
}
如果是寫在constructor.property上則可能寫到Object上污染object基類文章來源地址http://www.zghlxwxcb.cn/news/detail-631641.html
到了這里,關(guān)于Next.js使用裝飾器decorator 解決[作為表達(dá)式調(diào)用時,無法解析類修飾器的簽名。]的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!