// Vue 插件示例代码
// 定义一个插件
const MyPlugin = {
install(Vue, options) {
// 添加全局方法或属性
Vue.myGlobalMethod = function () {
// 逻辑...
console.log('This is a global method');
};
// 添加全局资源
Vue.directive('focus', {
inserted: function (el) {
el.focus();
}
});
// 注入组件选项
Vue.mixin({
created() {
console.log('This is a mixin');
}
});
// 添加实例方法
Vue.prototype.$myMethod = function (methodOptions) {
// 逻辑...
console.log('This is an instance method');
};
}
};
// 使用插件
new Vue({
el: '#app',
created() {
this.$myMethod(); // 调用实例方法
Vue.myGlobalMethod(); // 调用全局方法
}
});
// 如果需要在其他地方使用插件,可以导出
export default MyPlugin;
install
方法来定义插件。install
方法接收两个参数:Vue
构造器和 options
(可选配置)。Vue
上添加方法或属性,使其在整个应用中可用。v-focus
指令,当元素插入 DOM 后会自动聚焦。Vue.mixin
可以将选项注入到所有 Vue 组件中。Vue.prototype
添加方法,可以在每个 Vue 实例中调用。Vue.use(MyPlugin)
来安装插件,或者直接在创建 Vue 实例时使用插件中的功能。上一篇:vue2 ref
下一篇:vue项目搭建
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站