Vue.use(plugin) causing error: Vue is a constructor and should be called with the `new` keywor
Vue.use(plugin) causing error: Vue is a constructor and should be called with the `new` keywor
涉及插件 - https://www.npmjs.com/package/sweetalert
main.js
中的代码
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap-vue/dist/bootstrap-vue.css';
import BootstrapVue from 'bootstrap-vue';
import Vue from 'vue';
import Vuetify from 'vuetify';
import swal from 'sweetalert';
import App from './App';
import router from './router';
Vue.use(BootstrapVue);
Vue.use(Vuetify);
Vue.use(swal);
浏览器收到错误:
Uncaught SweetAlert: 1st argument ('function Vue (options) {
if ("development" !== 'production' &&
!(this instanceof Vue)
) {
warn('Vue is a constructor and should be called with the new
keyword');
}
this._init(options);
}') is invalid
如评论中所述,sweetalert
模块(来自 https://www.npmjs.com/package/sweetalert) is not a Vue plugin,因此您不能在...
中使用它
Vue.use(swal)
您可以做的是创建一个插件。例如,这会将 swal()
函数作为全局方法 (Vue.swal()
) 和实例方法 (this.$swal()
)
添加到 Vue
import Vue from 'vue'
import swal from 'sweetalert'
Vue.use({
// this is the required "install" method for Vue plugins
install (Vue) {
Vue.swal = swal
Vue.prototype.$swal = swal
}
})
我强烈建议使用现有的 Vue 插件,例如 vue-sweetalert2。
涉及插件 - https://www.npmjs.com/package/sweetalert
main.js
中的代码import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap-vue/dist/bootstrap-vue.css';
import BootstrapVue from 'bootstrap-vue';
import Vue from 'vue';
import Vuetify from 'vuetify';
import swal from 'sweetalert';
import App from './App';
import router from './router';
Vue.use(BootstrapVue);
Vue.use(Vuetify);
Vue.use(swal);
浏览器收到错误:
Uncaught SweetAlert: 1st argument ('function Vue (options) {
if ("development" !== 'production' &&
!(this instanceof Vue)
) {
warn('Vue is a constructor and should be called with thenew
keyword');
}
this._init(options);
}') is invalid
如评论中所述,sweetalert
模块(来自 https://www.npmjs.com/package/sweetalert) is not a Vue plugin,因此您不能在...
Vue.use(swal)
您可以做的是创建一个插件。例如,这会将 swal()
函数作为全局方法 (Vue.swal()
) 和实例方法 (this.$swal()
)
Vue
import Vue from 'vue'
import swal from 'sweetalert'
Vue.use({
// this is the required "install" method for Vue plugins
install (Vue) {
Vue.swal = swal
Vue.prototype.$swal = swal
}
})
我强烈建议使用现有的 Vue 插件,例如 vue-sweetalert2。