如何在Vue NuxtJS中将类正确设置为原型?
我创建插件
nuxt.config.js
plugins: [
{ src: "~/plugins/global.js" },
],
global.js
import Vue from "vue";
import CustomStore from "devextreme/data/custom_store";
//try set in prototype
Vue.use(CustomStore)
有错误
A class must be instantiated using the 'new'
我知道这是不正确的,但我找不到任何地方如何初始化它
Vue.use(new CustomStore());
没有错误,但如何调用?
我想在我的组件中使用这样的东西
this.dataSource = this.$CustomStore({ ///... settings...// })
我假设CustomStore
是一个函数,所以您可以尝试使用Nuxt.jsinject()
方法。此方法将使您的函数或值在应用程序中可用。
~/plugins/global.js
export default ({ app }, inject) => {
// Inject $CustomStore() in Vue, context and store.
inject('CustomStore', CustomStore)
}
然后你可以在你的应用程序组件中使用它。
您的组件
mounted() {
this.$CustomStore()
},
参考https://nuxtjs.org/docs/2.x/directory-structure/plugins#inject-在根目录中——上下文