本代码示例展示了如何使用 Vue.js 构建一个带有侧边栏的管理仪表盘。此类仪表盘广泛应用于各种管理系统中,为用户提供了一个简洁易用的界面来访问和管理系统功能。
此代码示例实现了以下基本功能:
侧边栏使用 <div>
元素构建,并设置了 class="w-64 bg-gray-50 border-r border-gray-200"
来定义其宽度、背景色和边框。
<div class="w-64 bg-gray-50 border-r border-gray-200">
...
</div>
菜单项使用 <ul>
和 <li>
元素创建,并设置了 class="space-y-2"
来定义菜单项之间的间距。
<ul class="space-y-2">
<li>
<a href="#" class="text-sm font-semibold text-gray-900">Dashboard</a>
</li>
...
</ul>
侧边栏折叠/展开按钮使用 <button>
元素创建,并设置了 class="text-sm font-semibold text-gray-900">+</button>
。
<div class="flex items-center justify-between p-4">
<div class="text-sm font-semibold text-gray-900">Menu</div>
<div>
<button class="text-sm font-semibold text-gray-900">+</button>
</div>
</div>
使用 Vue.js 的 ref
钩子创建了一个名为 count
的响应式变量,并使用 increment
和 decrement
方法来管理侧边栏的折叠/展开状态。
const count = ref(0)
const increment = () => {
count.value++
}
const decrement = () => {
count.value--
}
使用 v-bind
指令将 count
变量绑定到侧边栏的 class
属性,从而实现根据 count
的值动态切换侧边栏的折叠/展开状态。
<div :class="count === 0 ? 'w-0' : 'w-64'">
...
</div>