【发布时间】:2021-09-07 09:27:49
【问题描述】:
感谢@zim,我能够为两个在本地存储真/假的按钮简化代码。但是,由于某种原因,按钮单击有效,但它存储的是 [object MouseEvent] 而不是 True / False。我现在已经检查了很多次,但不知道为什么它没有存储正确的值。
标记
<div>
<button type="button" @click="clickPrivateChat">
<a key="privateChat" href="#" :class="privateChat?'bg-green-900 hover:bg-green-700':''" class="bg-red-900 text-gray-100 hover:bg-red-700 hover:text-white group w-full p-3 rounded-md flex flex-col items-center text-xs font-medium">
<ChatIcon class="h-6 w-6 text-white"/>
<span class="pt-2">Private Chat {{ privateChatOnOrOff }}</span>
</a>
</button>
</div>
<div>
<button type="button" @click="clickAllSounds">
<a key="privateChat" href="#" :class="allSounds?'bg-green-900 hover:bg-green-700':''" class="bg-red-900 text-gray-100 hover:bg-red-700 hover:text-white group w-full p-3 rounded-md flex flex-col items-center text-xs font-medium">
<VolumeUpIcon class="h-6 w-6 text-white"/>
<span class="pt-2">All Sounds {{ allSoundsOnOrOff }}</span>
</a>
</button>
</div>
脚本:
data() {
return {
privateChat: (localStorage.getItem("privateChat") === true) ? true : false,
allSounds: (localStorage.getItem("allSounds") === true) ? true : false, }
},
computed: {
privateChatOnOrOff() {
return this.privateChat ? 'ON' : 'OFF'
},
allSoundsOnOrOff() {
return this.allSounds ? 'ON' : 'OFF'
}
},
methods: {
clickPrivateChat (value) {
this.privateChat = !this.privateChat
localStorage.setItem("privateChat", value)
},
clickAllSounds (value) {
this.allSounds = !this.allSounds
localStorage.setItem("allSounds", value)
}
setup() {
const mobileMenuOpen = ref(false)
const privateChatEnabled = ref(privateChat)
let privateChatValue = localStorage.getItem("privateChat")
let privateChat = (privateChatValue === 'true')
const allSoundsEnabled = ref(allSounds)
let allSoundsValue = localStorage.getItem("allSounds")
let allSounds = (allSoundsValue === 'true')
return {
sidebarNavigation,
userNavigation,
mobileMenuOpen,
tabs,
userlists,
team,
activityItems,
privateChatEnabled,
allSoundsEnabled,
}
},
},
【问题讨论】:
标签: javascript vue.js tailwind-css vue-composition-api