我想要创建一个带有函数的Vue插件,该函数以编程方式呈现Vue组件。这个成分取决于Vuetify。如果我在组件中使用普通的HTML/CSS,一切都会很好,但是在组件中使用与Vuetify相关的东西(例如,a)是行不通的。我假设我没有正确地将vuetify注入到组件中。
在我的自定义组件中,我尝试分别导入每个Vuetify组件,但都没有成功。我还尝试使用语法: new ({vuetify})创建组件,但也没有成功。
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插件文件的代码:
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'
},
},
},
});发布于 2019-12-12 16:15:33
不确定您是否解决了这个问题,但我也遇到了相同的问题,即插件中的Vuetify不会被正确初始化。
Vuetify文档声明在创建vue实例时需要定义一个vuetify选项:
new Vue({
vuetify,
}).$mount('#app')幸运的是,自定义Vue插件有一个我们可以使用的选项参数。
下面是使用插件的代码:
const options = {}; // add other options here! (vuex, router etc.)
Vue.use(YourCustomPlugin, options);
new Vue(options).$mount('#app');这是你的插件代码:
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模块非常简单:
import Vuetify from "vuetify";
import "vuetify/dist/vuetify.min.css";
const opts = {}
export default new Vuetify(opts);发布于 2019-07-26 07:50:13
问题是您实际上没有在'@/plugins/vuetify'中导出插件本身;
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
}
}https://stackoverflow.com/questions/57215232
复制相似问题