【发布时间】:2019-12-05 03:21:25
【问题描述】:
我正在尝试在我的一个单文件组件中使用此 countdown-timer / 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
查看警告我发现了以下页面:
vue-router-problem1 vue-router-problem2
我从中收集/尝试的内容:
- 更改 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
- 使用 Vue.use(Plugin) 在 Main.js 中导入(同样的错误)
- 在路由器组件中导入(同样的错误)
编辑: 我也看过这个问题nested-components in vuejs 并像这样更改了组件注册:
beforeCreate() {
this.$options.components.CircularCountDownTimer = require('vue-circular-count-down-timer')
},
以上都没有让这个插件对我有用,我真的不明白为什么。
这是我的代码:
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");
组件(Visualization.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>
这也是从 localStorage 读取时的数据:
[{"id":1,"seconds":"60","name":"asdf"}]
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"
}
【问题讨论】:
-
为什么在
Vue.use(CircularCountDownTimer, "counter");中传递一个字符串"counter"?为什么不只是Vue.use(CircularCountDownTimer);? -
import CircularCountDownTimer from "vue-circular-count-down-timer";指向安装方法?它不是一个组件,是吗?
标签: vue.js vue-component