如何在我的自定义vue插件中使用“ Vue插件组件”

时间:2019-04-14 07:19:26

标签: javascript npm plugins vuejs2

我正在尝试在自己创建的自定义vue插件中使用vuetify组件,但似乎在应用程序“知道”该vuetify之前,我的插件渲染就存在了。

我试图在插件中使用Vue.use(Vuetify),但是它没有用,并且基本上,在我的插件中使用它没有任何意义,因为我希望插件用户(开发人员)将vuetify用作他的应用程序中的全局依赖项,因此它可以渗入我的插件

这是我的插件的vue html模板:

<v-content>
      <v-container fluid fill-height>
        <v-layout justify-center>
        <div v-if="videoLoading">
          <v-progress-circular v-if="useVuetifyLoader"
            :size="size"
            :width="width"
            :rotate="rotate" 
            :value="videoLoadingProgress*1.5"
            :color="color"
          >
            Downloading...
            {{ `${loadedInMB}/${totalInMb}Mb` }}
          </v-progress-circular>
          <div v-else>
              Downloading...
              {{ `${loadedInMB}/${totalInMb}Mb` }}
          </div>
        </div>
        <div v-else>
          <div>
            <div>foo</div>
          </div>
        </div>
      </v-layout>
    </v-container>
  </v-content>

这是我的插件:

import MyPlugin from '../lib/MyPlugin.vue';
import Vuetify from 'vuetify'


const install = function (Vue, config) {

Vue.component("my-plugin", MyPlugin)

}




export { install }

*and i tried also :*



 import MyPlugin from '../lib/MyPlugin.vue';
    import Vuetify from 'vuetify'


    const install = function (Vue, config) {
    Vue.use(Vuetify)// <-- did not work
    Vue.component("my-plugin", MyPlugin)

    }




    export { install }

,当我在其他应用中实现我的插件时,出现以下错误:

Unknown custom element: <v-content> - did you register the component correctly? For recursive components, make sure to provide the "name" option

 Unknown custom element: <v-container> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

Unknown custom element: <v-layout> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

Unknown custom element: <v-progress-circular> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

2 个答案:

答案 0 :(得分:0)

如何像这样设置插件并在其中包含自定义组件:

export default {
  install (Vue, options) {
     Vue.anyMethodYouLike = (value) => value

     Vue.component('v-content', Component);
     // Add `Vue.mixin()` to inject options to all components.
     Vue.mixin({
         // Add component lifecycle hooks and/or properties.
         created() {
         }
     });
     // We can also add Vue instance methods by attaching them to Vue.prototype.
     Vue.property.$myProperty = 'This is a Vue instance property.'
  }
}

然后在您的应用中使用,例如:

import myPlugin from './plugin.js' // the file with the code above

Vue.use(myPlugin, {
  someProp: 'blahdy blah'  //options to pass
})

答案 1 :(得分:0)

好的,所以我知道了。 我要做的就是在选项对象中将Vuetify作为参数传递,然后可以通过Vue.use(my_vuetify_param)在插件文件中使用它