[英]Vue3 composition API accordion menu, problem with showing data
I try to show the data when it is true, but it doesn't work.我尝试在数据为真时显示数据,但它不起作用。 I want to make an accordion menu (after clicking on a question, toggle an open property and show the answer if it's true)
我想做一个手风琴菜单(点击一个问题后,切换一个打开的属性并显示答案,如果它是真的)
Here is code:这是代码:
<div class="accordion">
<h1 class="title">Frequently asked questions</h1>
<div class="wrapper">
<div class="card" v-for="(item, i) in dataBase" :key="i">
<div class="card-header" @click="item.open = !item.open">
<h1>{{item.q}}</h1>
</div>
<div class="card-body" v-show="item.open">
{{item.d}}
</div>
</div>
</div>
And data:和数据:
const dataBase = [{
q: 'How much does it cost?',
d: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
open: false,
},
{
q: 'How it works?',
d: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
open: false,
}]
Can anyone help and explain why in the console log it changes to true => false and vice versa, but doesn't show the data when it's true?任何人都可以帮助解释为什么在控制台日志中它更改为 true => false 反之亦然,但在它为 true 时不显示数据? Im new in Vue.
我是 Vue 的新手。
your code is working bro.你的代码工作兄弟。 i runned on my computer.
我在我的电脑上运行。
if you asked 'why true->false and false->'.如果你问'为什么 true->false 和 false->'。 true and false is boolean variable.
true 和 false 是 boolean 变量。
<div class="card-header" @click="item.open = !item.open">
and this line you did你做的这条线
item.open = !item.open // if item.open is true then here is false.
item.open = !item.open // if item.open is false then here is true.
what is this exclamation(?) in vue ? vue 中的感叹号(?)是什么?
!(false) // true
!(true) // false
Your dataBase
property is not reactive, you should use ref
to make it reactive:您的
dataBase
属性不是反应性的,您应该使用ref
使其具有反应性:
import {ref} from 'vue'
const dataBase = ref([{
q: 'How much does it cost?',
d: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
open: false,
},
{
q: 'How it works?',
d: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
open: false,
}])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.