【问题标题】:Using the vue-konva with nuxtjs fails with various error使用带有 nuxtjs 的 vue-konva 失败并出现各种错误
【发布时间】:2021-11-04 12:13:56
【问题描述】:

我按照documentationGithub做了以下步骤:

  1. 使用npm install vue-konva konva --savenpm install canvas --save安装vue-konva和konva和canvas。

  2. plugins 文件夹下创建vuekonva.js,内容如下:

import Vue from 'vue'
import VueKonva from 'vue-konva'
Vue.use(VueKonva)
  1. nuxt.config.js下添加plugins: [ "~/plugins/vuekonva"],

  2. 我尝试在nuxt-config.js 下添加但仍然没有运气:

build: {
    standalone: true
  },
  1. pages 文件夹下创建了一个页面并从文档中添加了代码:
<template>
  <div>
    <v-stage ref="stage" :config="stageSize">
      <v-layer>
        <v-text :config="{ text: 'Some text on canvas', fontSize: 15 }" />
        <v-rect
          :config="{
            x: 20,
            y: 50,
            width: 100,
            height: 100,
            fill: 'red',
            shadowBlur: 10,
          }"
        />
        <v-circle
          :config="{
            x: 200,
            y: 100,
            radius: 50,
            fill: 'green',
          }"
        />
        <v-line
          :config="{
            x: 20,
            y: 200,
            points: [0, 0, 100, 0, 100, 100],
            tension: 0.5,
            closed: true,
            stroke: 'black',
            fillLinearGradientStartPoint: { x: -50, y: -50 },
            fillLinearGradientEndPoint: { x: 50, y: 50 },
            fillLinearGradientColorStops: [0, 'red', 1, 'yellow'],
          }"
        />
      </v-layer>
      <v-layer ref="dragLayer" />
    </v-stage>
  </div>
</template>

<script>
export default {
  data () {
    return {
      stageSize: {
        width,
        height
      }
    }
  },
  mounted () {
    if (process.browser) {
      this.stageSize.width = window.innerWidth
      this.stageSize.height = window.innerHeight
    }
  }
}
</script>

我得到错误: Must use import to load ES Module:

我在没有插件的情况下尝试过,它抛出了错误:

vue.runtime.esm.js:620 [Vue warn]: Unknown custom element: <v-stage> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

found in

不明白是什么问题,请帮忙。

【问题讨论】:

    标签: vue.js nuxt.js konvajs konva vue-konva


    【解决方案1】:

    根据Nuxt documentation,一些插件导出了一个 ES6 模块。我认为 konva 节点模块就是这种情况。我按照你上面提到的步骤。但在nuxt.config.js 文件中,我将插件配置如下:

    plugins: [    
        { src: "~/plugins/vuekonva", mode: 'client' }
    ],
    
    build: {
        transpile: ['konva']
    },

    之后我将你的页面代码替换为konvajs的代码如下:

    <template>
      <v-stage :config="configKonva">
        <v-layer>
          <v-circle :config="configCircle"></v-circle>
        </v-layer>
      </v-stage>
    </template>
    
    <script>
    export default {
      data() {
        return {
          configKonva: {
            width: 200,
            height: 200
          },
          configCircle: {
            x: 100,
            y: 100,
            radius: 70,
            fill: "red",
            stroke: "black",
            strokeWidth: 4
          }
        };
      }
    };
    
    </script>

    当我使用nuxt-link 链接到页面时,这对我有用。但是如果我刷新页面,我会收到一些可能是 SSR 的错误。我不确定,但如果您阅读this documentation,您也许可以解决 SSR 的问题。

    【讨论】:

    • 非常感谢您的回复。我一直在尝试各种事情,但没有运气。这对我帮助很大。再次感谢。祝你有美好的一天
    • 我认为你遇到的问题是因为 GitHub 页面很旧。我还注意到您在代码中使用了process.browser,我认为它是deprecated,必须替换为process.client
    • 非常感谢您的澄清。现在,一切似乎都正常了,所以从我的实际实现开始。
    • 你应该在这里使用mode: 'client'而不是ssr: false,因为它也被弃用了。
    • @kissu 感谢您的回复。根据您的建议进行了更改。
    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 2022-07-15
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2021-02-14
    • 2012-09-24
    相关资源
    最近更新 更多