console
new Vue({
el: '#app',
data: {
showMore: false,
userPermissions: ['button1', 'button3', 'button5', 'button7'],
allButtons: [
{ name: 'button1', action: () => { } },
{ name: 'button2', action: () => { } },
{ name: 'button3', action: () => { } },
{ name: 'button4', action: () => { } },
{ name: 'button5', action: () => { } },
{ name: 'button6', action: () => { } },
{ name: 'button7', action: () => { } },
{ name: 'button8', action: () => { } },
],
},
computed: {
toolbarButtons() {
const visibleButtons = this.allButtons.filter(button => this.userPermissions.includes(button.name));
if (visibleButtons.length > 3) {
return visibleButtons.slice(0, 3);
}
return visibleButtons;
},
moreButtons() {
return this.allButtons.filter(button => !this.toolbarButtons.includes(button));
},
},
});
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
<div class="toolbar">
<button v-for="button in toolbarButtons" :key="button.name" @click="button.action">{{ button.name }}</button>
<button v-if="moreButtons.length > 0" @click="showMore = !showMore">更多</button>
<div v-if="showMore" class="dropdown">
<button v-for="button in moreButtons" :key="button.name" @click="button.action">{{ button.name }}</button>
</div>
</div>
</div>
</body>
</html>