【发布时间】:2018-07-26 07:52:32
【问题描述】:
我尝试调整Vue unit testing guide,但在第一关卡住了。 Property 'created' does not exist on type 'VueConstructor<Vue>'.
这是我的测试,只是为了验证我的组件确实有一个 created 钩子:
import {assert} from 'chai';
import MyComponent from '../src/components/MyComponent.vue';
it('has a created hook', function() {
assert.equal(typeof MyComponent.created, 'function');
});
这是我正在测试的组件,MyComponent.vue:
<template>
</template>
<script lang="ts">
export default Vue.extend({
created() {
console.log("bye");
}
});
</script>
这个测试现在应该通过了。但是,我从 Typescript 编译器中得到一个错误。
$ karma start --single-run
15 02 2018 14:06:17.345:INFO [compiler.karma-typescript]: Compiling project using Typescript 2.6.2
15 02 2018 14:06:18.905:ERROR [compiler.karma-typescript]: test/vue_test.ts(5,37): error TS2339: Property 'created' does not exist on type 'VueConstructor<Vue>'.
我正在使用一个文件vue-shims.d.ts,它是这样的:
declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
【问题讨论】:
-
你可以试试
assert.equal(typeof MyComponent().created, 'function');。Vue.extend返回一个新的构造函数而不是一个对象 -
@TarunLalwani 这给了
Value of type 'VueConstructor<Vue>' is not callable。使用typeof (new MyComponent()).created得到Property 'created' does not exist on type 'CombinedVueInstance<Vue, object, object, object, Record<never, any>>'。 -
看来您在这里错误地使用了
Vue.extend。你需要像Vue.extend ({ methods: { created: () => "created" } } )一样使用它
标签: javascript typescript vue.js karma-runner