Next.js 会自动检测 jsconfig.json
或 tsconfig.json
中的experimentalDecorators
。
tsconfig.json
{"compilerOptions": {//..."experimentalDecorators": true}
}
然后重启服务 否则装饰器无法识别
不声明的话vscode 执行 ts检测 也会报错作为表达式调用时,无法解析类修饰器的签名。
类装饰器
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;
}