1、创建initList.js文件(存放公共方法)
import store from '@/store'
export default {
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>