【问题标题】:Nuxtjs directive can can't working v-can on v-btnNuxtjs 指令不能在 v-btn 上运行 v-can
【发布时间】:2021-10-23 00:12:25
【问题描述】:

我像这样创建一个插件permission.js

import Vue from "vue";
export default function({ store }) {
  Vue.directive("can", function(el, binding) {
    console.log(store.state.permissions.indexOf(binding.value) !== -1);  _//true_
    return store.state.permissions.indexOf(binding.value) !== -1;
  });
}

加载插件点赞

plugins: [{ src: "~/plugins/permission", ssr: true }],

并像这样使用

<button v-can="'add-customer'">You can edit this thing</button>

它不能正常工作。这里有什么问题?

【问题讨论】:

  • ssr: true 已弃用。现在是mode。但由于您希望在服务器和客户端上都使用它,因此您可以将其删除。

标签: vue.js plugins nuxt.js


【解决方案1】:

更改您的 permission.js 使其看起来像此代码。它可以正常工作。

import Vue from "vue";
export default function({ store }) {
  Vue.directive("can", function(el, binding, vnode) {
    if (store.state.permissions.indexOf(binding.value) === -1)
    {
      const comment = document.createComment(" ");
      Object.defineProperty(comment, "setAttribute", {
        value: () => undefined
      });
      vnode.elm = comment;
      vnode.text = " ";
      vnode.isComment = true;
      vnode.context = undefined;
      vnode.tag = undefined;
      vnode.data.directives = undefined;

      if (vnode.componentInstance) {
        vnode.componentInstance.$el = comment;
      }

      if (el.parentNode) {
        el.parentNode.replaceChild(comment, el);
      }
    }
  });
}

【讨论】:

  • 移除元素,使其看起来像 v-if。谢谢
猜你喜欢
  • 2018-12-08
  • 2021-08-20
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
  • 2021-06-05
  • 2020-03-21
  • 2021-05-02
  • 1970-01-01
相关资源
最近更新 更多