Vue学习——插件

功能:用于增强Vue
本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据。
定义:
    对象.install = function (Vue,options) {
        Vue.xxx
        Vue.xxx
        ...
    }
使用:Vue.use()

总结:其主要方法的使用是创建一个插件js文件,里面暴露出一个install方法并传入Vue参数,可以实现很多自定义的Vue方法,包括过滤器、mixin混入、自定义指令等,并且可以接收使用者传入的值,总体来说可用性很大,实现方式主要在main.js文件中引入,在vue文件中使用方法即可。

定义的一个过滤器插件

export default {
    install(Vue){
        Vue.filter('mySlice',function(value){
            return value.slice(0,4)
        })
    }
}

在main.js中引入

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
// 引入插件
import plugins from './plugins'
// 引入element-ui
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

// 应用plugins插件
Vue.use(plugins)
Vue.use(ElementUI)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>'
})

在自己的vue文件中使用

<template>
    <div class="blogger">
        <h1>{{msg}}</h1>
        <h2>博主尊称:{{name | mySlice}}</h2>
        <h2>博主性别:{{sex}}</h2>
        <el-button type="primary" icon="el-icon-s-promotion" @click="skip">点我跳转到博主博客</el-button>
    </div>
</template>

<script>

export default {
    name:'Blogger',
    data() {
        return {
            name:'才疏学浅的小缘同学',
            sex:'帅哥',
            msg:'青衫烟雨客,似是故人来',
        }
    },
    methods:{
        skip() {
        window.location.href="https://xiaoyuan.blog.csdn.net/"
        }
    }
}
</script>

<style>
    .blogger{
        text-align: center;
    }
</style>

实现效果,原本应该是才疏学浅的小缘同学过滤为才疏学浅

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

才疏学浅的小缘同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值