Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aurorame's avatar

vue spa hasrole

I make solution to check if user have role:

    methods: {
        $hasrole(rolename) {
            return Roles.indexOf(rolename) !== -1;
        },
    },
<div v-if="$hasrole('user')">You can see this div.</div>

But how i can edit function to give access to array of roles? $hasrole('user', 'guest', 'admin')

Or how i can change the logic of this?

0 likes
1 reply
bait-dept's avatar
Level 11

Your method needs to receive an array of roles and check if one of the roles received exist in your Roles array.

WARNING UNTESTED CODE

Roles: [
    'admin', 'user'
];

methods: {
    $hasrole(rolesname) {
        let exists = false;
        
        for (role of rolesname) {
            if (Roles.indexOf(role) !== -1) {
                exists = true;
                break;
            }
        }
        
        return exists;
    },
},

$hasrole(['user', 'admin']);

Please or to participate in this conversation.