Vue.js同级组件之间的通信,只有在插入卡片时才能改变卡片的视图?

问题描述 投票:0回答:1

我有一个问题,当我点击组件B上的插入文本按钮时,它会添加文本卡,也就是组件A......当插入卡时,我需要显示文本区域和保存按钮,然后点击保存后,文本区域必须隐藏,这就完成了,但问题是当重新加载页面时,文本区域和保存按钮又显示出来了......注意,A,B是兄弟组件

/组成部分A

<template>
  <div class="answer-text-container answer-inner-container">
    <div class="content-inner">
      <p class="answer-type">{{ text.type }}</p>
      <p v-show="!this.show" class="answer-text">{{ text.body }}</p>

      <textarea v-model="text.body" v-show="this.show" rows="3" class="edit-text-area border rounded" placeholder="Enter the text here"></textarea>
    </div>
    <div class="button-container" v-show="!this.show">
      <button type="button" @click="editText" class="btn btn-primary edit-text-btn">
        <i v-bind:class="[editTextString]"></i>
      </button>
      <button type="button" @click="$emit('delete')" class="btn btn-danger edit-text-btn">
        <i class="fas fa-trash-alt"></i>
      </button>
    </div>
    <button type="button" @click="editText" class="btn btn-success rounded" v-show="this.show">Save</button>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      show: 0,
      editTextString: "far fa-edit"
    };
  },
  props: ["text"],
  methods: {
    editText() {
      this.show = !this.show;
      console.log(this.text);
    },
    removeTextComp() {}
  }
};

/构成部分B

<template>
  <div class="toolbar-container mb-4">
    <div class="col-md-12 p-0 text-center">
      <div class="row mx-0">
        <div class="col-md-4 p-0 d-inline-block toolbar-sub-container">
          <a @click="insertText" href="#">
            <p>Insert Text</p>
          </a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data: function() {
    return {};
  },
  mounted() {},
  methods: {
    insertText() {
      this.$parent.insertTextComp();
    }
  }
};
</script>
javascript vue.js vuejs2 vue-component vuex
1个回答
1
投票

show:0 就是输入值。当你重新加载页面时,这个值也会重新加载。所以如果你想有一个实际的值 show 重载后使用localStorage或vuex store。 还有一个,我的意思是如果你的 show 值将是布尔值。

© www.soinside.com 2019 - 2024. All rights reserved.