如何使用vue-browser-acl在我的Vue项目中实现ACL?

问题描述 投票:1回答:1

使用此ACL library for Vue,我一直试图在2个文件中使用以下代码在my Vue-based project中实现ACL:

main.js

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import Acl from "vue-browser-acl";

const user = { name: "Raj", role: "normal" };

Vue.use(Acl, user, acl => {
  acl.rule("normal", "pages", user => user.role == "normal");
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

pages / PageStats.vue

<template>
  <div>
    <h1 v-role:normal>This is the stats page authenticated</h1>
    <h1>This is the stats page unauthenticated</h1>
  </div>
</template>

<script>
export default {};
</script>

<style scoped></style>

但是,即使用户具有role: "normal",我也看不到<h1 v-role:normal>标签。我在做什么错?

vue.js vue-component vuex vue-router acl
1个回答
1
投票

该库的文档非常混乱,但我认为要使用v-role指令,您需要configure a global rule

// remove the "pages" subject
acl.rule("normal", ({ role }) => role === "normal");

演示〜https://codesandbox.io/s/upbeat-lake-p1ndn


为了使用字符串主题,您似乎必须遵循一些字符串框规则。使用Pascal大小写的名称,例如"Pages"或将ACL配置为使用纯大小写模式

Vue.use(Acl, user, acl => {
  acl.rule("normal", ({ role }) => role === "normal");
  acl.rule("view", "pages");
}, {
  caseMode: false // 👈 configure plain case mode
});
<div v-role:normal>Normal role can view</div>
<div v-can:view="'pages'">Can view pages</div>

请参见https://github.com/mblarsen/vue-browser-acl#casemode

© www.soinside.com 2019 - 2024. All rights reserved.