插件 (功能集合)

功能介绍

本质上: 用于增强Vue, install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据

使用方法

1. 写一个 plugins/index.js , 配置一些资源挂载到 Vue 原型上

2. 在 main.js 中全局注册

2. 在别的组件中用 this. 访问

代码展示

# --------------------------------------plugins/index.js 中代码-------------------------------------------------
import Vue from "vue";
import axios from "axios";

export default {
    install(Vue, options) {

        // 添加全局属性
        Vue.prototype.$name = 'lqz'

        // 添加全局方法
        Vue.prototype.$myGlobalMethod = (a, b) => {
            return a + b
        }

        // 添加全局资源
        Vue.prototype.$axios = axios


        // 也可与添加 mixin -----> 全局组件
        Vue.mixin({
            data() {
                return {
                    name: 'pyy'
                }
            },
            methods: {
                handleClick() {
                    alert(this.name)
                }
            },
        })
    }
}

# ---------------------------------------main.js 代码----------------------------------------------

import plugins from './plugins'
Vue.use(plugins)


posted @ 2023-04-16 21:25  code455  阅读(25)  评论(0编辑  收藏  举报