爱学习的仔仔 2022-09-14 10:51 采纳率: 93.3%
浏览 121
已结题

数组对象怎么变字符串呀!

以下代码

<template>
  <div class="selector">
    <div class="selector-left">
      <div
        class="selector-left-tag"
        v-for="(tag, index) in tags"
        :key="index"
        @click="rightTransfer(tag, index)"
      >
        {{ tag.label }}
      </div>
    </div>
    <div class="selector-line"></div>
    <div class="selector-right">
      <div
        class="selector-right-tag"
        v-for="(sel, index) in selectedTags"
        :key="index"
        @click="letfTransfer(sel, index)"
      >
        {{ sel.label }}
      </div>
    </div>
    <div class="selector-line-btn">
      <button @click="sendTags">确认</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "tagSelect",
  props: {
    value: () => "",
  },
  data() {
    return {
      tags: [
        { label: "标签1", id: 1 },
        { label: "标签3", id: 3 },
        { label: "标签2", id: 2 },
        { label: "标签5", id: 5 },
        { label: "标签4", id: 4 },
        { label: "标签6", id: 6 },
      ],
      selectedTagIds: [],
      selectedTags: [],
    };
  },
  created() {
    this.tags.forEach((i, index) => {
      i.sortIndex = index;
      console.log(i);
    });
  },
  methods: {
    rightTransfer(val, index) {
      this.selectedTags.push({ ...val });
      this.tags.splice(index, 1);
      this.selectedTagIds.push(val.id);
    },
    letfTransfer(val, index) {
      // this.tags.push({ ...val });
      this.selectedTags.splice(index, 1);
      this.selectedTagIds.splice(index, 1);
      this.tags.splice(val.sortIndex, 0, val);
    },
    sendTags() {
      this.$emit("change", this.selectedTagIds, this.selectedTags);
    },
  },
};
</script>

<style lang="scss">
.selector {
  width: 500px;
  height: 300px;
  border: 1px solid #aaa;
  position: relative;
  display: flex;
  justify-content: space-between;
  .selector-left {
    margin-top: 50px;
    margin-left: 30px;
    cursor: pointer;
  }
  .selector-right {
    margin-top: 50px;
    margin-right: 36%;
    cursor: pointer;
  }
  .selector-line {
    position: absolute;
    top: 25px;
    left: 50%;
    width: 1px;
    height: 200px;
    background-color: #aaa;
  }
  .selector-line-btn {
    position: absolute;
    bottom: 50px;
    width: 500px;
    height: 1px;
    background-color: #aaa;
    & button {
      position: absolute;
      top: 10px;
      right: 10px;
      width: 50px;
      height: 30px;
    }
  }
}
</style>

img


选中的标签是数组包含对象的形式,我怎么才能把它变成字符串呀

  • 写回答

2条回答 默认 最新

  • 崽崽的谷雨 2022-09-14 11:07
    关注

    那你处理 一下呗 sel值 push或者 处理时 。只保留 label值 。
    letfTransfer 里 对 selectedTags 。再处理一下 。
    let arr=[];
    this. selectedTag.map((item)=>{
    arr.push(item.label)
    })

    this.this. selectedTag=arr

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月22日
  • 已采纳回答 9月14日
  • 创建了问题 9月14日

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启