英文:
Unknown VueKonva Type w/ TypeScript
问题
以下是您的翻译内容:
"I recently configured TypeScript for my Vue 2 project. Everything is working except for my VueKonva plugin - I receive the error below on app launch."
我最近为我的Vue 2项目配置了TypeScript。一切都正常,除了我的VueKonva插件 - 在应用启动时我收到以下错误。
"I tried adding 'vue-konva' to 'types' in 'tsconfig.json' but no luck. Creating a 'vue-konva.d.js' file also didn't help."
我尝试将'vue-konva'添加到'tsconfig.json'中的'types',但没有运气。创建'vue-konva.d.js'文件也没有帮助。
"Can anyone help me understand what I'm doing wrong?"
有人能帮助我理解我做错了什么吗?
"Failed to compile."
编译失败。
"main.ts"
main.ts
"import Vue from 'vue';"
从'vue'导入Vue;
"import App from '@/App.vue';"
从'@/App.vue'导入App;
"import VueKonva from 'vue-konva';"
从'vue-konva'导入VueKonva;
"Vue.use(VueKonva);"
Vue.use(VueKonva);
"new Vue({"
新Vue({
"VueKonva,"
VueKonva,
"render: h => h(App)"
render: h => h(App)
"}).$mount('#app');"
}).$mount('#app');
"tsconfig.js"
tsconfig.js
{
{
"compilerOptions": {
"compilerOptions": {
"target": "esnext","
"target": "esnext",
"module": "esnext","
"module": "esnext",
"strict": true,"
"strict": true,
"jsx": "preserve","
"jsx": "preserve",
"importHelpers": true,"
"importHelpers": true,
"moduleResolution": "node","
"moduleResolution": "node",
"skipLibCheck": true,"
"skipLibCheck": true,
"esModuleInterop": true,"
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,"
"allowSyntheticDefaultImports": true,
"sourceMap": true,"
"sourceMap": true,
"noImplicitAny": false,"
"noImplicitAny": false,
"allowJs": true,"
"allowJs": true,
"baseUrl": ".","
"baseUrl": ".",
"types": [
"types": [
"vue-konva","
"vue-konva",
"vuetify","
"vuetify",
"webpack-env"
"webpack-env"
]
],
"paths": {
"paths": {
"@/": [
"@//": [
"src/"
"src/"
]
}
],
"lib": [
"lib": [
"esnext","
"esnext",
"dom","
"dom",
"dom.iterable","
"dom.iterable",
"scripthost"
"scripthost"
]
}
}
},
"include": [
"include": [
"src//*.ts","
"src//*.ts",
"src//*.tsx","
"src//*.tsx",
"src//*.vue","
"src//*.vue",
"tests//*.ts","
"tests//*.ts",
"tests//*.tsx"
"tests//*.tsx"
]
],
"exclude": [
"exclude": [
"node_modules"
"node_modules"
]
}
}
英文:
I recently configured TypeScript for my Vue 2 project. Everything is working except for my VueKonva plugin - I receive the error below on app launch.
I tried adding "vue-konva",
to types
in tsconfig.json
but no luck. Creating a vue-konva.d.js
file also didn't help. Can anyone help me understand what I'm doing wrong?
Failed to compile.
src/main.ts:8:3
TS2769: No overload matches this call.
Overload 1 of 3, '(options?: ThisTypedComponentOptionsWithArrayProps<Vue<Record<string, any>, Record<string, any>, never, never, (event: string, ...args: any[]) => Vue<Record<string, any>, Record<string, any>, never, never, ...>>, ... 6 more ..., ComponentOptionsMixin> | undefined): CombinedVueInstance<...>', gave the following error.
Argument of type '{ VueKonva: PluginObject<{}> | PluginFunction<{}>; render: (h: CreateElement) => VNode; }' is not assignable to parameter of type 'ThisTypedComponentOptionsWithArrayProps<Vue<Record<string, any>, Record<string, any>, never, never, (event: string, ...args: any[]) => Vue<Record<string, any>, Record<string, any>, never, never, ...>>, ... 6 more ..., ComponentOptionsMixin>'.
Object literal may only specify known properties, and 'VueKonva' does not exist in type 'ThisTypedComponentOptionsWithArrayProps<Vue<Record<string, any>, Record<string, any>, never, never, (event: string, ...args: any[]) => Vue<Record<string, any>, Record<string, any>, never, never, ...>>, ... 6 more ..., ComponentOptionsMixin>'.
Overload 2 of 3, '(options?: ThisTypedComponentOptionsWithRecordProps<Vue<Record<string, any>, Record<string, any>, never, never, (event: string, ...args: any[]) => Vue<Record<string, any>, Record<string, any>, never, never, ...>>, ... 6 more ..., ComponentOptionsMixin> | undefined): CombinedVueInstance<...>', gave the following error.
Argument of type '{ VueKonva: PluginObject<{}> | PluginFunction<{}>; render: (h: CreateElement) => VNode; }' is not assignable to parameter of type 'ThisTypedComponentOptionsWithRecordProps<Vue<Record<string, any>, Record<string, any>, never, never, (event: string, ...args: any[]) => Vue<Record<string, any>, Record<string, any>, never, never, ...>>, ... 6 more ..., ComponentOptionsMixin>'.
Object literal may only specify known properties, and 'VueKonva' does not exist in type 'ThisTypedComponentOptionsWithRecordProps<Vue<Record<string, any>, Record<string, any>, never, never, (event: string, ...args: any[]) => Vue<Record<string, any>, Record<string, any>, never, never, ...>>, ... 6 more ..., ComponentOptionsMixin>'.
Overload 3 of 3, '(options?: ComponentOptions<Vue<Record<string, any>, Record<string, any>, never, never, (event: string, ...args: any[]) => Vue<Record<string, any>, Record<string, any>, never, never, ...>>, ... 7 more ..., ComponentOptionsMixin> | undefined): CombinedVueInstance<...>', gave the following error.
Argument of type '{ VueKonva: PluginObject<{}> | PluginFunction<{}>; render: (h: CreateElement) => VNode; }' is not assignable to parameter of type 'ComponentOptions<Vue<Record<string, any>, Record<string, any>, never, never, (event: string, ...args: any[]) => Vue<Record<string, any>, Record<string, any>, never, never, ...>>, ... 7 more ..., ComponentOptionsMixin>'.
Object literal may only specify known properties, and 'VueKonva' does not exist in type 'ComponentOptions<Vue<Record<string, any>, Record<string, any>, never, never, (event: string, ...args: any[]) => Vue<Record<string, any>, Record<string, any>, never, never, ...>>, ... 7 more ..., ComponentOptionsMixin>'.
6 |
7 | new Vue({
> 8 | VueKonva,
| ^^^^^^^^
9 | render: h => h(App)
10 | }).$mount("#app");
11 |
main.ts
import Vue from "vue";
import App from "@/App.vue";
import VueKonva from 'vue-konva';
Vue.use(VueKonva);
new Vue({
VueKonva,
render: h => h(App)
}).$mount("#app");
tsconfig.js
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"noImplicitAny": false,
"allowJs": true,
"baseUrl": ".",
"types": [
"vue-konva",
"vuetify",
"webpack-env"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
答案1
得分: 1
错误信息指向了您的问题。
请查阅项目文档以了解如何使用VueKonva。
以下是适用于Vue v3的工作示例:
const { createApp } = Vue
const app = createApp({
data() {
return {
configKonva: {
width: 200,
height: 200,
},
configCircle: {
x: 100,
y: 100,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
}
}
}
});
app.use(VueKonva);
app.mount('#app');
<div id="app">
<v-stage ref="stage" :config="configKonva">
<v-layer ref="layer">
<v-circle :config="configCircle"></v-circle>
</v-layer>
</v-stage>
</div>
<!--1. Link Vue Javascript & Konva-->
<script src="https://unpkg.com/vue@3.2.47/dist/vue.global.prod.js"></script>
<script src="https://unpkg.com/konva@8.4.3/konva.min.js"></script>
<!--2. Link VueKonva Javascript -->
<script src="https://unpkg.com/vue-konva@3.0.2/umd/vue-konva.js"></script>
更新
- Vue v2没有
$mount
方法。 - 不要将插件放在App定义内。
以下是适用于Vue v2的工作示例:
Vue.use(VueKonva)
new Vue({
el: '#app',
data() {
return {
configKonva: {
width: 200,
height: 200,
},
configCircle: {
x: 100,
y: 100,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
}
}
}
})
<div id="app">
<v-stage ref="stage" :config="configKonva">
<v-layer ref="layer">
<v-circle :config="configCircle"></v-circle>
</v-layer>
</v-stage>
</div>
<!--1. Link Vue Javascript & Konva-->
<script src="https://unpkg.com/konva@8.4.3/konva.min.js"></script>
<script src="https://unpkg.com/vue@2.7.14/dist/vue.min.js"></script>
<!--2. Link VueKonva Javascript -->
<script src="https://unpkg.com/vue-konva@2.1.7/umd/vue-konva.js"></script>
英文:
The error message points correctly at your problem.
> 8 | VueKonva,
| ^^^^^^^^
Check the project documentation on how to use VueKonva.
import { createApp } from 'vue';
import App from './App.vue';
import VueKonva from 'vue-konva';
const app = createApp(App);
app.use(VueKonva);
app.mount('#app');
Here is the working example for Vue v3
<!-- begin snippet: js hide: true console: false babel: false -->
<!-- language: lang-js -->
const { createApp } = Vue
const app = createApp({
data() {
return {
configKonva: {
width: 200,
height: 200,
},
configCircle: {
x: 100,
y: 100,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
}
}
}});
app.use(VueKonva);
app.mount('#app');
<!-- language: lang-html -->
<div id="app">
<v-stage ref="stage" :config="configKonva">
<v-layer ref="layer">
<v-circle :config="configCircle"></v-circle>
</v-layer>
</v-stage>
</div>
<!--1. Link Vue Javascript & Konva-->
<script src="https://unpkg.com/vue@3.2.47/dist/vue.global.prod.js"></script>
<script src="https://unpkg.com/konva@8.4.3/konva.min.js"></script>
<!--2. Link VueKonva Javascript -->
<script src="https://unpkg.com/vue-konva@3.0.2/umd/vue-konva.js"></script>
<!-- end snippet -->
UPDATE
- Vue v2 has no
$mount
method. - Do not put the plugin inside the App definition
Your code
new Vue({
// this is not necessary -> VueKonva,
render: h => h(App)
}) // this is not necessary -> .$mount("#app");
Here is the working example for Vue v2
<!-- begin snippet: js hide: true console: false babel: false -->
<!-- language: lang-js -->
Vue.use(VueKonva)
new Vue({
el: '#app',
data() {
return {
configKonva: {
width: 200,
height: 200,
},
configCircle: {
x: 100,
y: 100,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
}
}
}
})
<!-- language: lang-html -->
<div id="app">
<v-stage ref="stage" :config="configKonva">
<v-layer ref="layer">
<v-circle :config="configCircle"></v-circle>
</v-layer>
</v-stage>
</div>
<!--1. Link Vue Javascript & Konva-->
<script src="https://unpkg.com/konva@8.4.3/konva.min.js"></script>
<script src="https://unpkg.com/vue@2.7.14/dist/vue.min.js"></script>
<!--2. Link VueKonva Javascript -->
<script src="https://unpkg.com/vue-konva@2.1.7/umd/vue-konva.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论