vue3案例

代码
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

<template>
  <div class="app-page">
    <div
      v-for="title in data.titleArr"
      :key="title.id"
      class="title-box"
      @click="onClick(title)"
    >
      <span>{{ title.name }}</span>
      <img v-if="title.active" class="dh-img" src="对号图片地址" />
    </div>

    <div v-if="isShow">我是标题2的弹框</div>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
let isShow = ref(false);
let data = reactive({
  titleArr: [
    { id: 1, name: "标题1" },
    { id: 2, name: "标题2" },
  ],
});

const onClick = (item) => {
  let arr = data.titleArr.map((el) => {
    Object.assign(el, { active: false });
  });
  console.log(arr);
  data.titleArr.forEach((el) => {
    if (el.name == item.name) {
      el.active = true;
    }
  });
  if (item.id == 2) {
    isShow.value = !isShow.value;
  } else if (item.id == 1) {
    isShow.value = false;
  }
};
</script>



<style scoped>
.app-page {
  text-align: center;
}
.title-box {
  border: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  padding: 10px;
}
.title-box:first-child {
  border-bottom: none;
}
.dh-img {
  width: 20px;
  height: 20px;
}
</style>

</style>

其中遇到问题:

const data = reactive({
	arr: []
})

报错:Uncaught TypeError: Cannot read properties of undefined (reading ‘forEach/map/filter‘) proxy
解决方案:

const arr = reactive([])

奇妙的是: 第二天我换成了第一次写法居然没有报错,不知道为什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值