欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

插件:用于增强Vue

本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据。

定义插件:install 可传递多个参数

 

对象.install = function(Vue,options){

  //定义全局过滤器

  Vue.filter(......)

  //添加全局指令

  Vue.directive(......)

  //配置全局混入

  Vue.mixin(......)

  //添加实例方法

  Vue.prototype.$mymethod= function(){......}

  Vue.prototype.$myProperty=xxx

}

使用插件:Vue.use()

示例一:

plugins.js

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// eslint-disable-next-line no-unused-vars
export default
  {
    install (Vue) {
      console.log('install log', Vue)
 
      // 全局过滤器
      Vue.filter('mySlice', function (value) {
        return value.slice(0, 4)//字符串截取
      })
 
      // --------------全局自定义指令开始--------------
      Vue.directive('fbindobjall', {
        // 指令与元素成功绑定时(一上来)
        bind (element, binding) {
          console.log('bind fbindobjall')
          element.value = binding.value
        },
        // 指令所在元素被插入页面时
        // eslint-disable-next-line no-unused-vars
        inserted (element, binding) {
          console.log('inserted fbindobjall',)
          element.focus()
        },
        // 指令所在的模版被重新解析时
        update (element, binding) {
          console.log('update fbindobjall')
          element.value = binding.value
          // element.focus()
        }
      })
 
      // 定义混入
      Vue.mixin({
        data () {
          return {
            x: 100,
            y: 200
          }
        },
      })
 
      // 给Vue原型上添加一个方法(vm和vc就都能用了)
      Vue.prototype.hello = () => {
        alert('你好vue')
      }
 
 
    }
  }
 
// export default obj  默认暴露

 

Student.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!-- 组件的结构 -->
<template>
  <div class="demo">
    <h3>学校姓名:{{name}}</h3>
    <h3>学校地址:{{ address|mySlice }}</h3>
    <button @click="test()">查询提示</button>
 
  </div>
</template>
 
<!-- 组件交互相关的代码(数据、方法等) -->
<script>
export default ({
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Student',
  data () {
    return {
      name: '高新一小',
      address: '西安/高新一小'
    }
  },
  methods: {
    test () {
      this.hello()
    }
  }
 
})
</script>
 
<!-- 组件的样式 -->
<style>
.demo {
  background-color: burlywood;
}
</style>

School.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!-- 组件的结构 -->
<template>
  <div class="demo">
    <h3>学校姓名:{{name}}</h3>
    <h3>学校地址:{{ address|mySlice }}</h3>
    <button @click="test()">查询提示</button>
 
  </div>
</template>
 
<!-- 组件交互相关的代码(数据、方法等) -->
<script>
export default ({
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Student',
  data () {
    return {
      name: '高新一小',
      address: '西安/高新一小'
    }
  },
  methods: {
    test () {
      this.hello()
    }
  }
 
})
</script>
 
<!-- 组件的样式 -->
<style>
.demo {
  background-color: burlywood;
}
</style>

App.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<template>
  <div>
    <!-- <img src="./assets/logo.png"> -->
 
    <Student></Student>
    <hr>
    <School></School>
 
  </div>
</template>
 
<script>
// 引入组件
import Student from './components/Student.vue';
import School from './components/School.vue';
export default {
  name: 'App',
  components: {
    Student,
    School
  },
}
</script>
 
<style>
</style>

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 引入Vue
import Vue from 'vue'
// 引入App
import App from './App.vue'
// 引入插件
import plugins from './plugins'
// 应用插件
Vue.use(plugins)
// 配置提示
Vue.config.productionTip = false
 
new Vue({
  render: h => h(App),
}).$mount('#app')

 

posted on   sunwugang  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示