欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue根據(jù)權(quán)限動(dòng)態(tài)渲染按鈕、組件等的函數(shù)式組件實(shí)現(xiàn)

 更新時(shí)間:2023年11月09日 10:25:28   作者:donggua_123  
這篇文章主要介紹了vue根據(jù)權(quán)限動(dòng)態(tài)渲染按鈕、組件等的函數(shù)式組件實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望杜大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

前端工作中對于權(quán)限的需求是難免的,最近項(xiàng)目中就有這樣的需求了。

后臺管理員可以新增前臺成員,不通的成員具有不同的權(quán)限,有權(quán)限的人員可以看到一些操作按鈕或者數(shù)據(jù)列表,反之則看不到。

那么也就是說,前端需要根據(jù)登錄用戶的權(quán)限來渲染指定的dom。

1、components文件下新建authorized文件夾

文件夾下新建auth.js校驗(yàn)權(quán)限

const checkAuth = (authName) => {
    let authMap = ['admin', 'superAdmin', 'admin-1', 'landq'] // 根據(jù)實(shí)際項(xiàng)目需求配置
    return authMap.includes(authName);
}
export default checkAuth;

這里的authMap可以是從后臺動(dòng)態(tài)請求的,也可以是前端配置好的,甚至可以是其它格式的數(shù)據(jù),根據(jù)各人業(yè)務(wù)需求處理,大致思路是一樣的。

2、authorized文件夾下新建index.vue

<!-- 函數(shù)式組件 -->
<script>
import checkAuthorized from './auth';
export default {
    functional: true, // 函數(shù)式組件
    props: {
        // 接收參數(shù)
        authName: {
            type: String,
            required: true,
            default: ''
        }
    },
    render(h, context) {
        let { props, scopedSlots } = context;
        return checkAuthorized(props.authName)
            ? scopedSlots.default({
                  name: props.authName
              })
            : null;
    }
};
</script>

這里接收一個(gè)參數(shù),登錄用戶名,在render函數(shù)里做進(jìn)一步判斷,如果權(quán)限為true則返回默認(rèn)插槽里的dom,即渲染指定元素,反之返回null,不渲染

3、全局注冊main.js

、、、
import Authorized from '@/components/authorized'; // 權(quán)限
 
Vue.config.productionTip = false;
Vue.component('Authorized', Authorized);
 
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

4、使用

<!-- authorized -->
<template>
    <div class="authorized">
        <Authorized authName="admin">
            <el-button type="primary" slot-scope="data">{{
                data.name
            }}</el-button>
        </Authorized>
    </div>
</template>
<script>
export default {
    components: {},
    data() {
        return {};
    },
    created() {},
    //生命周期 - 掛載完成(可以訪問DOM元素)
    mounted() {},
    computed: {},
    //方法集合
    methods: {}
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css類
</style>

當(dāng)authName='admin'時(shí),admin在authMap數(shù)組中,按鈕渲染了

當(dāng)authName='123'時(shí)便不會(huì)渲染

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論