vue添加全局方法

1、创建initList.js文件(存放公共方法)

import store from '@/store'

export default {
  /**
   * 按钮权限
   * code 就是权限表的code, 多个权限需要传入数组
   * type只有在多个权限的时候生效, 支持 some(默认) 或者 every
   */
  showElement(code, type = 'some') {
    const menuAuth = store.getters.menuAuth
    return menuAuth.includes(code)
  },

  /**
   * 获取时间
   */
  getTime() {
    const date = new Date()
    const month = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    const day = date.getDate() <= 9 ? '0' + date.getDate() : date.getDate()
    const value = date.getFullYear() + '0' + month + '-' + day
    return value
  }
}

2、创建index.js文件(引用公共方法)

import initList from './initList' // 公共方法

const list = []

for (const n in initList) { // 公共方法
  list.push({
    name: '$' + n,
    value: initList[n]
  })
}

function addPrototypeFunction(vue) {
  list.forEach(item => {
    vue.prototype[item.name] = item.value
  })
}

export default addPrototypeFunction

3、注册到Vue中(main.js)

import addPrototypeFunction from './init/index'
addPrototypeFunction(Vue)

4、使用(js中使用)

created() {
  console.log('time => ', this.$getTime())
},

5、vue中使用

<el-tabs v-model="activeName">
  <el-tab-pane v-if="$showElement(1100)" label="设备列表" name="list" />
</el-tabs>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值