Unknown VueKonva Type w/ TypeScript

huangapple go评论63阅读模式
英文:

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>

更新

  1. Vue v2没有$mount方法。
  2. 不要将插件放在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.

  &gt;  8 |   VueKonva,
       |   ^^^^^^^^

Check the project documentation on how to use VueKonva.

import { createApp } from &#39;vue&#39;;
import App from &#39;./App.vue&#39;;
import VueKonva from &#39;vue-konva&#39;;

const app = createApp(App);
app.use(VueKonva);
app.mount(&#39;#app&#39;);

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: &#39;red&#39;,
      stroke: &#39;black&#39;,
      strokeWidth: 4,
    }
  }
}});
app.use(VueKonva);
app.mount(&#39;#app&#39;);

<!-- language: lang-html -->

&lt;div id=&quot;app&quot;&gt;
  &lt;v-stage ref=&quot;stage&quot; :config=&quot;configKonva&quot;&gt;
    &lt;v-layer ref=&quot;layer&quot;&gt;
      &lt;v-circle :config=&quot;configCircle&quot;&gt;&lt;/v-circle&gt;
    &lt;/v-layer&gt;
  &lt;/v-stage&gt;
&lt;/div&gt;
&lt;!--1. Link Vue Javascript &amp; Konva--&gt;
&lt;script src=&quot;https://unpkg.com/vue@3.2.47/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/konva@8.4.3/konva.min.js&quot;&gt;&lt;/script&gt;
&lt;!--2. Link VueKonva Javascript --&gt;
&lt;script src=&quot;https://unpkg.com/vue-konva@3.0.2/umd/vue-konva.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

UPDATE

  1. Vue v2 has no $mount method.
  2. Do not put the plugin inside the App definition

Your code

new Vue({
  // this is not necessary -&gt; VueKonva,
  render: h =&gt; h(App)
}) // this is not necessary -&gt; .$mount(&quot;#app&quot;);

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: &#39;#app&#39;,
  data() {
    return {
      configKonva: {
        width: 200,
        height: 200,
      },
      configCircle: {
        x: 100,
        y: 100,
        radius: 70,
        fill: &#39;red&#39;,
        stroke: &#39;black&#39;,
        strokeWidth: 4,
      }
    }
  }
})

<!-- language: lang-html -->

&lt;div id=&quot;app&quot;&gt;
  &lt;v-stage ref=&quot;stage&quot; :config=&quot;configKonva&quot;&gt;
    &lt;v-layer ref=&quot;layer&quot;&gt;
      &lt;v-circle :config=&quot;configCircle&quot;&gt;&lt;/v-circle&gt;
    &lt;/v-layer&gt;
  &lt;/v-stage&gt;
&lt;/div&gt;
&lt;!--1. Link Vue Javascript &amp; Konva--&gt;
&lt;script src=&quot;https://unpkg.com/konva@8.4.3/konva.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/vue@2.7.14/dist/vue.min.js&quot;&gt;&lt;/script&gt;
&lt;!--2. Link VueKonva Javascript --&gt;
&lt;script src=&quot;https://unpkg.com/vue-konva@2.1.7/umd/vue-konva.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月31日 02:40:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定