简体   繁体   English

如何正确使用 Vue 插件?<PluginName> 没有定义

[英]How to use Vue Plugins Correctly? <PluginName> is not defined

Im learning to make Vue Plugin, based on https://v2.vuejs.org/v2/guide/plugins.html , this is my simple code:我正在学习制作 Vue 插件,基于https://v2.vuejs.org/v2/guide/plugins.html ,这是我的简单代码:

plugin1.js:插件1.js:

AlertPlugin.install = function (Vue, options) {
    Vue.prototype.$classicalert = function (message) {
        alert(message)
    };
};

app.js:应用程序.js:

window.Vue = require('vue');
import AlertPlugin from './plugin1.js'
Vue.use(AlertPlugin);

const app = new Vue({
    el: '#app',
    render: h => h(Main)
});

when im trying to run it, the web page become blank, and error AlertPlugin is not defined .当我尝试运行它时,网页变为空白,并且错误AlertPlugin 未定义

please help?请帮忙?

In your plugin1.js file, you are attempting to set the install property of the AlertPlugin object, which (as the error says) is not defined.在您的plugin1.js文件中,您正在尝试设置AlertPlugin对象的install属性,该对象(如错误所示)未定义。

Your plugin1.js file should look like this:您的plugin1.js文件应如下所示:

export default {
  install: function (Vue, options) {
    Vue.prototype.$classicalert = function (message) {
      alert(message)
    };
  }
}

This defines a default object to export containing a property install .这定义了一个要导出的default对象,其中包含一个属性install When you import this object as AlertPlugin , like you are doing in app.js , it will result in an AlertPlugin object with the install property you defined in the plugin's file.当您将此对象导入为AlertPlugin时,就像您在app.js中所做的那样,它将生成一个带有您在插件文件中定义的install属性的AlertPlugin对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM