【发布时间】:2021-02-25 19:25:59
【问题描述】:
我的这个 Vue 插件不工作:
import _Vue from "vue";
import particles from "./Particles.vue";
const VueParticles = (Vue: typeof _Vue, options: unknown) => {
_Vue.component('Particles', particles);
};
export { particles as ParticlesComponent };
export default VueParticles;
它可以构建,但如果我尝试使用它,它不会加载组件并且应用程序会返回此错误:
[Vue 警告]:未知的自定义元素:- 您是否正确注册了组件?对于递归组件,请确保提供“名称”选项。
发现于
---> 在 src/App.vue
我像这样加载插件:
import Particles from "particles.vue";
Vue.use(Particles);
但如果我使用Vue.component 语法加载组件,它就可以正常工作,如下所示:
import { ParticlesComponent } from "particles.vue";
Vue.component("Particles", ParticlesComponent);
这是我正在使用的模板:
<Particles id="tsparticles" :options="options" :particlesInit="particlesInit" :particlesLoaded="particlesLoaded"/>
您可以尝试按照以下步骤复制问题:
- 克隆tsParticles
dev分支:git clone https://github.com/matteobruni/tsparticles.git --branch dev - 运行
yarn && npx lerna bootstrap && npx lerna run build - 转到
demo/vue文件夹 - 运行
yarn serve并打开http://localhost:8080,一切正常(动画背景应该开始动画) - 编辑
src/App.vue评论工作Vue.component并恢复Vue.use - 重新运行
yarn serve并打开http://localhost:8080,这次后台没有出现
我刚刚从 yarn 工作区切换到标准 yarn 以解决整个项目中节点依赖关系的大问题
我不明白它为什么会这样坏。
我还尝试了一个外部 Vue.js 应用程序,而不是项目内的演示应用程序,但没有任何改变。
该组件正在使用vue-property-decorator,但我尝试切换到Vue.extend 语法,但没有任何改变,所以我恢复到以前的类代码。
【问题讨论】:
-
你为什么用
_Vue而不是Vue -
只是一个类型,我改成
Vue,变量名改成了vue,但是什么都没变
标签: javascript typescript vue.js vuejs2