代码之家  ›  专栏  ›  技术社区  ›  Puchacz

注释类

  •  1
  • Puchacz  · 技术社区  · 6 年前

    我想在javascript项目中使用注释类。我正在使用webpack,一切看起来都正常(项目编译),但带注释的类无法执行。

    function annotation(target) {
        target.isAnnotated = true;
    }
    
    @annotation()
    export class Template {
        constructor(id, name, subject, type) {
            this.id = id;
            this.name = name;
            this.subject = subject;
            this.type = type;
            this.message = '';
        }
    }
    

    {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        query: {
            plugins: [
                ["@babel/plugin-proposal-decorators", { "legacy": true }]
            ]
        }
    }
    

    但是在运行代码时,我得到一个异常:

    Cannot set property 'isAnnotated' of undefined
    

    要么我不能正确地创建注释函数,要么还有一些东西需要配置,但是基于 this

    0 回复  |  直到 6 年前