【问题标题】:pass change of prop from parent to child with change Vue 3 Option api使用更改 Vue 3 Option api 将道具的更改从父级传递给子级
【发布时间】:2021-12-28 15:17:00
【问题描述】:

好吧...国王..

当其他组件发生单击事件时,我试图将值传递给子组件,它将更改父组件的道具值。但它只显示了第一个通过的挂载值。

发出变化的自顶向下组件

<template>
<div class="dropdown">
    <button
      class="btn btn-secondary dropdown-toggle h-75"
      type="button"
      id="dropdownMenuButton1"
      data-bs-toggle="dropdown"
      aria-expanded="false"
    >
      {{ value }}
    </button>
    <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1" role="menu">
      <li v-for="option in options" :key="option">
        <a
          class="dropdown-item"
          @click="(value = option);dunder;dundet;"
          href="javascript:void(0)"
          >{{ option }}</a
        >
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "TopDown",
  data() {
    return {
      options: ["Edit", "Delete"],
      value: "",
    };
  },
  computed:{
    dunder(){
     return this.$emit("edit-task", this.value) 

    },
    dundet(){
      return this.$emit("edit-task-index",this.value)
    }
  }
};
</script>


<style>
</style>

父组件

<template>
  <div
    v-for="(item, index) in tasker"
    :items="item"
    :key="index"
    class="border border-dark "
  >
    <section class="d-flex justify-content-between">
      <h4 class="w-50 font-weight-bold fs-5">
        <u>{{ item.title }}</u>
      </h4>
      <TopDown
        @edit-task="val"
        @edit-task-of="show(item, index)"
        
        :index="index"
      />
    </section>
    <p class="text-start">{{ item.description }}</p>
    <GoTask :showVal="showVal" :bool="bool" />
  </div>
</template>

<script>
import TopDown from "@/components/TopDown.vue";
import GoTask from "@/components/GoTask.vue";

export default {
  inheritAttrs: false,
  components: {
    TopDown,
    GoTask,
  },
  data() {
    return {
      takss: {
        items: "sss",
        index: "",
      },
      showVal: "",
      bool: false,
    };
  },
  name: "Taski",
  props: ["tasker"],
  methods: {
    show(item, index) {
      this.takss.items = item;
      this.takss.index = index;
          
    },
    val(val) {
      if (val == "Edit") {
        setTimeout(() => {
          console.log(this.takss);
          this.showval = this.takss;
          console.log(this.showval);
          console.log(this.bool)
        }, 1000);
          this.bool = !this.bool;
      }
    },
  },
};
</script>

<style></style>


子组件

<template>
  <section
    v-if="bools"
    class="bg-white"
    style="z-index: 20"
    @click="closeModal"
  ></section>
  <section
    v-if="bools"
    class="position-absolute"
    style="
      z-index: 50;
      left: 50%;
      top: 50%;
      height: 100vh;
      margin-top: 20vh;
    "
  >
    <form
      class="mt-10"
      @submit.prevent="editTask"
    >
      <input
        class="border rounded p-2 mb-2"
        v-model.lazy="newTask.title"
        placeholder="Title"
        type="text"
      />
      <textarea
        ref="texsearch"
        rows="20"
        class=" p-2 mb-2"
        v-model.lazy="newTask.description"
        placeholder="Task Details"
        type="text"
      ></textarea>
      <button
        class="border rounded p-2 bg-success text-white"
        type="submit"
        @submit.prevent=""
      >
        New Task
      </button>
    </form>
  </section>
  <button @click="test">dd</button>
</template>

<script>
export default {
  inheritAttrs: false,
  name: "GoTask",
  props: ["tasker", "showVal", "bool"],
  data() {
    return {
      showVals: this.showVal,
      bools: this.bool,
      newTask: {
        title: "",
        description: "",
      },
    };
  },
  methods: {
    test() {
      console.log(this.bools);
      console.log(this.showVal);
    },
    ModalOpen() {
      this.bools = true;
    },
    closeModal() {
      this.bools = false;
    },
    showModal() {
      this.bools = true;
      // auto focus
      this.$nextTick(() => {
        this.$refs.textsearch.focus();
      });
    },
    showtheVal() {
      console.log(this.showtheVal);
    },
  },
};
</script>

<style></style>


当我单击其他组件的按钮时,它会发出 @edit-task-of 和 @edit-task 的变化,它不会将 bool 和 showval 的新值发送到子组件,bool 值仍然为 false 与当单击运行测试功能的按钮时,首先安装和 showVal = "" 以在孩子处查看这两个值。我对vue完全陌生。谢谢

【问题讨论】:

  • 分享TopDown组件
  • 添加了topdown组件王

标签: javascript vue.js


【解决方案1】:

您在GoTask 组件,方法test 中犯了错误,首先console.log(this.bools);bools 是本地状态,在prop bool 更改后未更新,只需正确console.log(this.bool);。不需要将 props 设置为本地状态,直接使用 props 即可。 GoTask 正确例子:

props: ["tasker", "showVal", "bool"],
data() {
    return {
      newTask: {
        title: "",
        description: "",
      },
    };
},
methods: {
    test() {
      console.log(this.bool);
      console.log(this.showVal);
    },

codesandbox

【讨论】:

  • 非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2021-06-29
  • 2017-12-24
  • 2019-10-28
  • 2021-10-03
  • 2023-01-13
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
相关资源
最近更新 更多