應用場景
以后臺管理系統為例,每個用戶所擁有的按鈕權限不一樣。管理員配置權限之后,用戶登錄時,從接口拿到按鈕權限列表,然后根據后臺數據判斷顯示哪些按鈕。
Vue.js官網關于自定義指令的講解
基礎概念
Vue除了核心功能默認內置的指令(v-model和v-show)還可以注冊自定義指令。
在Vue2.0中,代碼復用和抽象的主要形式是組件。但有的情況下,仍需要對普通DOM元素進行底層操作,這時候就會用到自定義指令。
譬如,自定義一個v-focus指令,當頁面加載時,輸入框將獲得焦點
1
|
< input v-focus> |
全局自定義
1
2
3
4
5
6
7
8
|
// 注冊一個全局自定義指令 `v-focus` Vue.directive( 'focus' , { // 當被綁定的元素插入到 DOM 中時…… inserted: function (el) { // 聚焦元素 el.focus() } }) |
局部自定義
1
2
3
4
5
6
7
8
9
|
//如果想注冊局部指令,組件中也接受一個 directives 的選項: directives: { focus: { // 指令的定義 inserted: function (el) { el.focus() } } } |
鉤子函數
一個指令定義對象可以提供如下幾個鉤子函數(均為可選)
bind
只調用一次,指令第一次綁定到元素時調用。在這里可以進行一次性的初始化設置。
inserted
被綁定元素插入父節點時調用(僅保證父節點存在,但不一定已被插入文檔中)
update
所在組件的VNode更新時調用,但是可能發生在其子VNode更新之前。指令的值可能發生了改變,也可能沒有。但是你可以通過比較更新前后的值來忽略不必要的模板更新。
componentUpdated
指令所在組件的VNode及其子VNode全部更新后調用。
unbind
只調用一次,指令與元素解綁時調用。
其他
除此之外,還有一些基礎概念,鉤子函數參數,動態指令參數,等等。
官網講的十分清楚,在此不再多做贅述。
原理
如果對自定義指令的源碼感興趣,博客園也有一篇文章講的很通透
http://www.jfrwli.cn/article/228464.html
原理就是:
- 解析Vue的屬性的時候,遍歷每個屬性;
- 對象上增加一個directives屬性保存指令信息;
- 渲染完成后會執行directives模塊的create鉤子函數;
- Vue 編譯生成的虛擬節點,也就是VNode插入到父節點后,
- 依次執行每個函數,也就執行到我們自定義定義的inserted鉤子函數了
自定義指令v-has
話休絮煩,言歸正傳。
今天主要是總結一下:自定義指令v-has,按鈕權限判斷
登錄接口拿到按鈕權限列表,存入本地緩存LOGIN_USER_BUTTON_AUTH中
數據格式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[ { "checked" : false , "component" : "" , "createTime" : "2019-06-29 18:21:06" , "createUser" : "026a564bbfd84861ac4b65393644beef" , "icon" : "" , "id" : "1503273153861066776" , "name" : "今日采集(案卷)" , "open" : "true" , "parentId" : "2328050996633395469" , "parentName" : "首頁" , "permission" : "sys:index:vol" , "sort" :103, "status" : "0" , "type" : "2" , "updateTime" : "2021-01-27 15:51:15" , "updateUser" : "026a564bbfd84861ac4b65393644beef" , "url" : "" } ] |
自定義v-has指令的配置
在utils文件夾下,新建hasPermission.js文件,在index.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
|
const hasPermission = { install (Vue, options) { Vue.directive( 'has' , { inserted: (el, binding, vnode)=>{ filterGlobalPermission(el, binding, vnode); } }); } }; /** * 全局權限控制 */ export const filterGlobalPermission = (el, binding, vnode) => { let permissionList = []; let authList = JSON.parse(localStorage.getItem( 'LOGIN_USER_BUTTON_AUTH' ) || "[]" ); for (let auth of authList) { permissionList.push(auth); } if (!permissionList.length) { el.parentNode.removeChild(el); return ; } let permissions = []; for (let item of permissionList) { permissions.push(item.permission); } if (!permissions.includes(binding.value)) { el.parentNode.removeChild(el); } } export default hasPermission; |
utils文件下的index.js
utils文件夾下的其他js文件也可以統一在index.js里導出
1
2
|
import hasPermission from './hasPermission' export { hasPermission } |
main.js中引入
1
2
|
import { hasPermission } from '@/utils' Vue.use(hasPermission) |
組件中使用v-has根據按鈕權限,判斷是否顯示該按鈕
1
2
3
|
< el-button v-has = "'sys:arch:add'" type = "primary" size = "mini" icon = "el-icon-plus" @ click = "add('1')" > 新增 </ el-button > |
總結
到此這篇關于Vue自定義v-has指令實現按鈕權限判斷的文章就介紹到這了,更多相關Vue自定義v-has指令內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/6950482024368963597