首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我的自定义vue插件中注入vue

如何在我的自定义vue插件中注入vue
EN

Stack Overflow用户
提问于 2019-07-26 07:34:42
回答 2查看 2.9K关注 0票数 3

我想要创建一个带有函数的Vue插件,该函数以编程方式呈现Vue组件。这个成分取决于Vuetify。如果我在组件中使用普通的HTML/CSS,一切都会很好,但是在组件中使用与Vuetify相关的东西(例如,a)是行不通的。我假设我没有正确地将vuetify注入到组件中。

在我的自定义组件中,我尝试分别导入每个Vuetify组件,但都没有成功。我还尝试使用语法: new ({vuetify})创建组件,但也没有成功。

代码语言:javascript
复制
import MyCustomComponent from '@/components/MyCustomComponent'
import vuetify from '@/plugins/vuetify';



export default {
  install(Vue, options) {
    function renderMyCustomComponent() {
        const CustomComponent= Vue.extend(MyCustomComponent)
        Vue.use(vuetify)
        let instance = new CustomComponent()
        instance.$mount()
        document.body.appendChild(instance.$el)
    }

    Vue.prototype.$renderMyComponent = renderMyCustomComponent
  }

}

错误消息表明,vuetify (或至少其中一些属性)在组件[Vue warn]: Error in getter for watcher "isDark": "TypeError: Cannot read property 'dark' of undefined"中不可用

提示/编辑:我正在使用Vuatefie2.0。Vuetify被注入应用程序的方式发生了一些变化。下面是我的vuetify插件文件的代码:

代码语言:javascript
复制
import Vue from 'vue';
import Vuetify from 'vuetify';
import 'vuetify/dist/vuetify.min.css';
import de from 'vuetify/es5/locale/de';

Vue.use(Vuetify)

export default new Vuetify({
  theme: {
    themes: {
      light: {
        primary: '#3f51b5',
        secondary: '#b0bec5',
        accent: '#8c9eff',
        error: '#b71c1c'
      },
    },
  },
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-12 16:15:33

不确定您是否解决了这个问题,但我也遇到了相同的问题,即插件中的Vuetify不会被正确初始化。

Vuetify文档声明在创建vue实例时需要定义一个vuetify选项:

代码语言:javascript
复制
new Vue({
  vuetify,
}).$mount('#app')

幸运的是,自定义Vue插件有一个我们可以使用的选项参数。

下面是使用插件的代码:

代码语言:javascript
复制
const options = {}; // add other options here! (vuex, router etc.)
Vue.use(YourCustomPlugin, options);

new Vue(options).$mount('#app');

这是你的插件代码:

代码语言:javascript
复制
import vuetify from "./src/plugins/vuetify";

export default {
    install(Vue, options) { // options is undefined unless you pass the options param!
        Vue.component('my-custom-component', MyCustomComponent);
        Vue.use(Vuetify);
        options.vuetify = vuetify;
    }
};

vuetify模块非常简单:

代码语言:javascript
复制
import Vuetify from "vuetify";
import "vuetify/dist/vuetify.min.css";

const opts = {}

export default new Vuetify(opts);
票数 6
EN

Stack Overflow用户

发布于 2019-07-26 07:50:13

问题是您实际上没有在'@/plugins/vuetify'中导出插件本身;

代码语言:javascript
复制
import MyCustomComponent from '@/components/MyCustomComponent'
import Vuetify from 'vuetify';

export default {
  install(Vue, options) {
    function renderMyCustomComponent() {
        Vue.use(Vuetify)
        const CustomComponent= Vue.extend(MyCustomComponent)
        let instance = new CustomComponent()
        instance.$mount()
        document.body.appendChild(instance.$el)
    }

    Vue.prototype.$renderMyComponent = renderMyCustomComponent
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57215232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档