mixins.ts
//导出混合方法
export function mix(...args){
return function (t){
Object.assign(t.prototype,...args);//添加方法到对象原型
}
}
test_mixins.ts
import { mix} from './mixins';//导入混合方法
//声明要混合入类的方法
let methodToMix = {
MixFunc(){console.log('this method use for Mix');}
}
@mix(methodToMix)//在类声明中使用了修饰器@mix
class MyClass{}
let obj = new MyClass();//实例化类,类对象具有修饰器混入的方法MixFunc
obj.MixFunc();//调用混入的方法
测试结果 :