3
我正在寫vue的自定義指令。自定義指令類似於v-如果在vuejs
我希望它能像v-if
一樣工作,但它內部會有一點邏輯。讓我以一個例子來解釋:
<button v-permission="PermissionFoo">Do Foo</button>
它會檢查權限並顯示或隱藏組件。
目前我通過CSS樣式這樣做:
var processPermissionDirective = function (el, binding, vnode) {
if (SOME_LOGIC_HERE) {
el.style.display = el._display;
}
else {
el.style.display = 'none';
}
}
export default {
bind: function (el, binding, vnode) {
el._display = el.style.display;
processPermissionDirective(el, binding, vnode);
},
update: function (el, binding, vnode) {
processPermissionDirective(el, binding, vnode);
}
}
但我不希望這件留在文檔中。所以我正在尋找除CSS之外的另一種方式,因爲它也必須從DOM中刪除,例如v-if
。
這是一個很好的答案,但它會在控制檯中顯示'不能設置屬性'$ el'undefined'。 – EasonBlack
它工作的很好,當我刪除vnode.componentInstance。$ el = comment; 在我的情況下,因爲componentInstance未定義。 謝謝 – EasonBlack