首页 > 其他分享 >Vue 中plugins插件的使用

Vue 中plugins插件的使用

时间:2022-12-04 13:34:36浏览次数:54  
标签:插件 vue element Vue install plugins


Vue 中plugins插件的使用


1:说明

/*
## 插件

1. 功能:用于增强Vue

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

3. 定义插件:

    ```js
    对象.install = function (Vue, options) {
        // 1. 添加全局过滤器
        Vue.filter(....)

        // 2. 添加全局指令
        Vue.directive(....)

        // 3. 配置全局混入(合)
        Vue.mixin(....)

        // 4. 添加实例方法
        Vue.prototype.$myMethod = function () {...}
        Vue.prototype.$myProperty = xxxx
    }
    ```

4. 使用插件:```Vue.use()```



*/


2:代码结构

image

3:代码内容


vue.config.js

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave:false /*关闭语法检查*/
})



main.js

 //引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//关闭Vue生产提示
Vue.config.productionTip=false;

// 引入插件
import plugins  from './plugins'
//应用插件
Vue.use(plugins)

// 创建Vm
const vm = new Vue(  {
        el:'#app',
        render: (h) => h(App)
   });

index.html

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <!-- 针对ie浏览器的一个特殊配置,含义是让ie浏览器以最高的渲染级别进行渲染界面 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 开启移动端理想视口 -->
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <!-- 配置页签的图标 -->
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <!-- 配置网页的标题:找 package.json文件里的 "name": "vue_test" 值 -->
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <!-- 如果浏览器不支持js,则该标签的元素里的内容将会被渲染 -->
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <!-- 容器 -->
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>



App.vue


 <!--

 -->

<template>
  <div>
    <hr/>
    <div class="divCss">
        <h1 class="h1Css">Vue中的 plugins插件</h1>
        <hr/>
        <!-- 调用组件,传递数据 -->
        <School/>
        <hr/>
        <Student />
        <hr/>

    </div>

  </div>
</template>

<script>

    import School from './components/School.vue';
    import Student from './components/Student.vue';


    export default {
        name:'App',
        components:{
            Student,
            School
        },
    }
</script>
<style>
   .divCss{
        background-color: chocolate;
        margin: auto;
        padding: 20px;
   }
   .h1Css{
        font-size: 36px;
        color: white;
   }

plugins.js


/*
## 插件

1. 功能:用于增强Vue

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

3. 定义插件:

    ```js
    对象.install = function (Vue, options) {
        // 1. 添加全局过滤器
        Vue.filter(....)

        // 2. 添加全局指令
        Vue.directive(....)

        // 3. 配置全局混入(合)
        Vue.mixin(....)

        // 4. 添加实例方法
        Vue.prototype.$myMethod = function () {...}
        Vue.prototype.$myProperty = xxxx
    }
    ```

4. 使用插件:```Vue.use()```



*/

export default{
    /* Vue入参 为vue 对象的构造器 */
    install(Vue){
        console.log("@@@@@@@@@@@@@@@@@@@@@@install@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",Vue);
        //全局过滤器
        Vue.filter('mySlice',function(value){
            return value.slice(0,4)
        }) ;
        //全局自定义指令
        Vue.directive("fbind",{
            //指令与元素成功绑定时(一上来)
            bind(element,binding){
                //console.log("fbind.bind",this);//注意此处的this是window
                element.value = binding.value;
            },
            //指令所在元素被插入页面时
            inserted(element,binding){
                //console.log("fbind.inserted",this);//注意此处的this是window
                element.focus();
            },
            //指令所在的模板被重新解析时
            update(element,binding){
                //console.log("fbind.update",this);//注意此处的this是window
                element.value = binding.value;
            }
        })
        Vue.directive('big1',function(element,binding){
            //console.log('big1',this);//注意此处的this是window
            element.innerText=binding.value * 1000;
        });
        // 全局混入
        Vue.mixin({
            data () {
                return {
                    x:100,
                    y:100
                }
            }
        })
        // 给Vue原型上添加1个方法
        Vue.prototype.hello=()=>{alert('你好啊!')}

    }

}


Student.vue

<!--

 -->
<template>
    <div  class="schoolClassStyle">
      <h1  style="color:red">{{msg}}</h1>
      <h2  >学生名称:{{name}}</h2>
      <h2>学生性别:{{sex}}</h2>
      <input type="text" v-fbind:value="name"  style="margin-left:10px;"/><span style="color:dimgrey;font-size: 9px;padding: 20px;margin-left: 20px;">测试插件的全局绑定 </span>
      <button @click="clickHelloInfo">点击测试插件的属性hello方法</button>
      <br/>
    </div>
</template>

  <script>
      export default {
        name:'Student',
        data () {
          return {
            msg:'学生信息',
            name:'向北',
            sex:'男'
          }
        },
        methods:{
          clickHelloInfo(){
            this.hello()
          }
        }

      }
  </script>

  <style>
    .schoolClassStyle{
      background-color: aquamarine;
    }
  </style>

School.vue

<!--

 -->
<template>
    <div  class="schoolClassStyle">
      <h1  style="color:red">{{msg}}</h1>
      <h2  >学校名称:{{name | mySlice}}<span style="color:dimgrey;font-size: 9px;padding: 20px;margin-left: 20px;">      测试插件中全局管道符</span></h2><!-- 给学校名称属性name 增加1个 mySlice的过滤器属性 -->
      <h2>学校地址:{{address}}</h2>
    </div>
</template>

  <script>
      export default {
        name:'School',
        data () {
          return {
            msg:'学校信息',
            name:'华南师范大学',
            address:'广州市天河区中山大道西55号'
          }
        },

      }
  </script>

  <style>
    .schoolClassStyle{
      background-color: aquamarine;
    }
  </style>
















4:界面效果图


imageimageimageimageimage






标签:插件,vue,element,Vue,install,plugins
From: https://www.cnblogs.com/ios9/p/16949725.html

相关文章

  • Vue 中 的 mixin 混入(合)
    Vue中的mixin混入(合)1:说明/*##mixin(混入)1.功能:可以把多个组件共用的配置提取成一个混入对象2.使用方式:第一步定义混合:```{dat......
  • vue_登录
    <template><!--根节点--><divclass="login-container"><divclass="login-form-wrap"><!--el-form表单组件每个表单项都必须......
  • 老板:你为什么要选择 Vue?
    大家好,我是Kagol,VueDevUI开源组件库和EditorX富文本编辑器创建者,专注于前端组件库建设和开源社区运营。假如你是团队的前端负责人,现在老板要拓展新业务,需要开发一个......
  • IDE常用插件
    1、MaterialThemeUI该插件的作用在于能够提供多种不同的页面风格。2、Statisticstatistic项目统计插件,统计整体代码量,包括所有文件的统计数量和行数。3、Tabnine......
  • UML语法 - 插件的安装和使用
    UML介绍https://plantuml.com/zh/sequence-diagramPlantUML是一个可以让你快速编写UML图的组件,语法可以直接参考这个网站的语法示例画图。在线UML方便快捷:http://www......
  • Vue-treeselect 实现下拉树懒加载,末节点不要箭头
    项目需要,可选择的下拉树,由于数据过多,显示要有层级感,所以使用了懒加载模式vue-treeselect官网:https://www.vue-treeselect.cn/1、前端代码1、下载依赖npminstall--sav......
  • Vue2(笔记16) - Vue核心 - 内置指令
    回顾下之前的指令:v-bind  :单向绑定解析表达式,可简写:xxxv-model:双向数据绑定;v-for   :遍历数组/对象/字符串v-on   :绑定事件监听,可简写 @v-if    :条件......
  • 高效率工具安利(软件,插件,油猴)
    目录电脑软件:edge插件:油猴脚本:电脑软件:Snipaste-截图+贴图超级超级好用的截屏软件,随时随地截屏,还有回溯截屏历史,截图贴在屏幕上等诸多超级实用的功能u......
  • gulp涉及的自动化构建的插件gulp-load-plugins,gulp-sass,gulp-imagemin等以及启动服
    学习的过程中,给我最明显的感受就是如果版本过新,会导致很多不必须要的麻烦(各种缺modules以及依赖),把趟过的路记录一下,以及一个工程的规划设计工程想要使用gulp,需要gu......
  • arco design vue 表单自定义验证
    不知道为啥,官方文档里竟然没写...直接上代码template里<a-form-itemfield="repeatPassword":rules="[{validator:validateRepeatPassword,trigger:'change'}]"......