我有这个代码:
const c = Vue.extend({
data() {
return {
msg: 'Hello'
}
},
methods: {
greet(): string {
this.msg = "asdasd"; // Type '"asdasd"' is not assignable to type 'Function'.
return this.msg + ' world';
}
}
});
但this.msg = "asdasd"
显示错误:Type '"asdasd"' is not assignable to type 'Function'.
。这在VS和webpack构建期间都会发生。感觉就像打字稿不能正确看到这种类型,即使我使用Vue.extend
这可能是什么原因?
我的tsconfig.json
:
{
"compilerOptions": {
"outDir": "./built/",
"sourceMap": true,
"strict": true,
"noImplicitReturns": true,
"module": "es2015",
"moduleResolution": "node",
"target": "es5",
"allowSyntheticDefaultImports": true,
},
"include": [
"./ViewModels/**/*"
]
}
我还从.csproj
文件中删除了所有提到的typescript,因为VS无法构建typescript(我不想要它,因为我使用webpack来构建它)
Webpack配置:https://gist.github.com/archeg/743348021c59563a9d479993bc0c349f
对于那些正在努力解决同一问题的人:我切换到https://github.com/vuejs/vue-class-component
,它开始正常工作。
例如,我发布的代码与vue-class-component
一样:
import Vue from "vue";
import Component from 'vue-class-component';
@Component({})
class Example extends Vue {
msg = "Hello";
greet(): void {
this.msg = "asdasd"; //This is a proper here.
return this.msg + ' world';
}
}