在没有根vue js文件的情况下将插件作为“ Vue.use()”添加到vu实例

时间:2020-02-20 09:37:59

标签: javascript vue.js instance directus

我正在从事Directus项目。 Directus是用PHP和Vue.js制作的无头CMS。

我想在自定义页面上使用主题或类似Vuetify的插件。 插件通常这样称呼:

import Vue from 'vue'; // Something like that

Vue.use(...)

但是我无权访问实例化Vue的文件。

您知道我是否可以从子文件项目添加插件和主题吗?怎么样?

在此先感谢您的答复!

顺便说一句,

尼古拉斯

1 个答案:

答案 0 :(得分:3)

是的,您可以在创建Vue实例后添加插件。

请参见下面的示例:

<template>
  <div>
    <button type="button" @click="installPlugin">install plugin</button>
    <button type="button" @click="callPlugin">call plugin</button>
  </div>
</template>

<script>
export default {
  name: 'app',
  methods: {
    installPlugin() {
      this.$root.constructor.use({
        install(Vue, options) {
          console.log('plugin was installed');
          Vue.prototype.$test = () => console.log('plugin was called');
        },
      });
    },
    callPlugin() {
      this.$test();
    },
  },
};
</script>

在此示例中,该插件已安装并在子组件中调用。