我有此代码:
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/743348021c59563a9d479999999999993bc0c349f
对于那些在同一问题上挣扎的人:我切换到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';
}
}