无法安装组件:模板或渲染函数未定义。(Vue使用插件)

Hac*_*XIt 4 vue.js vue-component

我正在尝试在我的单文件组件之一中使用这个倒计时器/ on-github 。即使我像示例中提到的那样导入它,我仍然收到此错误:

21:27:20.553 [Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <CircularCountDownTimer>
       <Visualization> at src/views/Visualization.vue
         <App> at src/App.vue
           <Root> vue.runtime.esm.js:619
    VueJS 17
    run es6.promise.js:75
    notify es6.promise.js:92
    flush _microtask.js:18

Run Code Online (Sandbox Code Playgroud)

查找警告我发现了以下页面:

vue-路由器问题1 vue-路由器问题2

我从中收集/尝试的内容:

  • 更改 vue-cli 配置以使用运行时编译器(无更改)
22:02:49.722 [Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <CircularCountDownTimer>
       <Visualization> at src/views/Visualization.vue
         <App> at src/App.vue
           <Root> vue.esm.js:628
    VueJS 18
    run es6.promise.js:75
    notify es6.promise.js:92
    flush _microtask.js:18

Run Code Online (Sandbox Code Playgroud)
  • 使用 Vue.use(Plugin) 在 Main.js 中导入(同样的错误)
  • 将其导入路由器组件中(同样的错误)

编辑: 我还查看了这个问题nested-components in vuejs 并更改了组件注册,如下所示:

    beforeCreate() {
      this.$options.components.CircularCountDownTimer = require('vue-circular-count-down-timer')
    },
Run Code Online (Sandbox Code Playgroud)

上述任何一个都没有使这个插件对我有用,我真的不明白为什么。

这是我的代码:

main.js

    import Vue from "vue";
    import App from "./App.vue";
    import router from "./router";
    import CircularCountDownTimer from "vue-circular-count-down-timer";

    Vue.use(CircularCountDownTimer)

    Vue.config.productionTip = false;

    export const eventBus = new Vue();

    new Vue({
      router,
      render: h => h(App)
    }).$mount("#app");

Run Code Online (Sandbox Code Playgroud)

组件(可视化.vue):

<template>
  <div id="content">
    <circular-count-down-timer
      v-for="counter in counters" :key="counter.id"
      :initial-value="counter.seconds"
      :show-minute="false"
      :show-hour="false"
      :show-negatives="false"
      :second-label="counter.name"
      :steps="1"
    />
  </div>
</template>
<script>
import CircularCountDownTimer from "vue-circular-count-down-timer";
export default {
  name: "Visualization",
  components: {
    CircularCountDownTimer
  },
  data() {
    return {
      counters: []
    }
  },
  mounted() {
    if (localStorage.getItem("delays")) {
      try {
        this.counters = JSON.parse(localStorage.getItem("delays"));
      } catch (e) {
        console.debug(e);
        localStorage.removeItem("delays");
      }
    }
  }
};
</script>
Run Code Online (Sandbox Code Playgroud)

这也是从 localStorage 读取时的数据:

[{"id":1,"seconds":"60","name":"asdf"}]
Run Code Online (Sandbox Code Playgroud)

package.json 中的依赖项:

    "dependencies": {
        "@babel/preset-env": "^7.5.4",
        "core-js": "^2.6.5",
        "vue": "^2.6.10",
        "vue-awesome-countdown": "^1.0.16",
        "vue-circular-count-down-timer": "^1.0.4",
        "vue-grid-layout": "^2.3.4",
        "vue-router": "^3.0.3"
      }
Run Code Online (Sandbox Code Playgroud)

ski*_*tle 5

vue-circular-count-down-timer是一个插件,所以这段代码似乎是正确的:

import CircularCountDownTimer from "vue-circular-count-down-timer";

Vue.use(CircularCountDownTimer)
Run Code Online (Sandbox Code Playgroud)

如果您查看该插件的源代码,您会发现它所做的只是全局注册一个名为 的组件circular-count-down-timer

https://github.com/noorzaie/vue-circular-count-down-timer/blob/master/src/components/index.js

当您执行以下操作时会出现问题:

import CircularCountDownTimer from "vue-circular-count-down-timer";
export default {
  name: "Visualization",
  components: {
    CircularCountDownTimer
  },
Run Code Online (Sandbox Code Playgroud)

您只需再次导入插件,然后尝试将其用作组件。但它不是一个组件,而是一个插件。Vue 不知道这一点,它只是看到一个没有templateorrender函数的对象。

摆脱本地组件导入,它应该只使用全局注册的组件。