javascript - VueJS 改变重复的 v 模型(:value, @input)

标签 javascript vue.js emit v-model

我在自己的组件中使用 v-model 时遇到问题。也就是说,我不想使用 State 或 Bus。 目前,该组件在 App.js 中正确返回单个值,它会重复自身。 我无法处理它,请帮助并解释我的问题。

非常感谢!

应用程序.js:

<template>
  <b-container>
    <SectionSelector :AddSection="AddSection"/>
      <component 
          v-for="(section, index) in sections"
          :key="index"
          :is="section.type"
          :sectionIndex="index"
          :sectionData="section[index]"
          @sectionDataEmit="sectionDataEmit"/>
  </b-container>
</template>

<script>
  import SectionSelector from './components/SectionSelector.vue';
  import FullText from './components/sections/FullText.vue';
  import FullImage from './components/sections/FullImage.vue';
  import ImageRightTextLeft from './components/sections/ImageRightTextLeft.vue';
  import ImageLeftTextRight from './components/sections/ImageLeftTextRight.vue';

  export default {
    data() {
      return {
        sections: []
      }
    },
    methods: {
      AddSection(sectionData) {
        this.sections.push(sectionData);
      },
      updateSection(sectionIndex, sectionData) {
        this.sections[sectionIndex] = sectionData;
      },
      sectionDataEmit(emitData) {
        // eslint-disable-next-line
        console.log(emitData.position, emitData.content);
        this.sections[emitData.position].fields.text = emitData.content;
      }
    },
    components: {
      SectionSelector,
      FullText,
      FullImage,
      ImageRightTextLeft,
      ImageLeftTextRight
    }
  }
</script>

SectionSelector.vue:

<template>
  <b-row>
        <b-dropdown id="dropdown-1" text="Select" class="m-md-2">
          <b-dropdown-item v-for="(section, index) in sections"
                          :key="index"
                          @click="PushSection(index)"> {{ section.type }} </b-dropdown-item>
        </b-dropdown>
    </b-row>
</template>

<script>
  export default {
    props: ['AddSection'],
    data() {
      return {
        sections: [
          { 
            type: 'FullText',
            fields: {
              text: ''
            }
          },
          { 
            type: 'FullImage',
            fields: {
              url:''
            }
          },
          { 
            type: 'ImageRightTextLeft',
            fields: {
              img: '',
              text: ''
            }
          },
          { 
            type: 'ImageLeftTextRight',
            fields: {
              img: '',
              text: ''
            }
          }
        ]
      }
    },
    methods: {
      PushSection(index) {
        this.AddSection(this.sections[index])
      }
    }
  }
</script>

FullText.vue:

<template>
  <b-row>
    <h3>Full text {{ sectionIndex+1 }}</h3>
    <b-textarea
    :value="sectionData" 
    @input="sectionDataEmit"></b-textarea>
  </b-row>
</template>

<script>
  export default {
    props: ['sectionIndex', 'sectionData'],
    methods: {
      sectionDataEmit(value) {
        let emitData = {
          position: this.sectionIndex,
          content: value
        }
        this.$emit('sectionDataEmit', emitData)
      }
    }
  }
</script>

目前代码会导致sections.fields.text重复(在chrome扩展Vue中可见)

最佳答案

您的代码中有一个地方使用了object[index]=。不要对 Vue 数据对象这样做。而是使用 Vue.set(object, index, value)

updateSection(sectionIndex, sectionData) {
        Vue.set(sections,sectionIndex, sectionData);
      },

这是基于以下规则:在初始化 data 后,Vue 无法对添加到对象的新属性使用react。

关于javascript - VueJS 改变重复的 v 模型(:value, @input),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321226/

相关文章:

javascript - 为什么这个文件系统 api requestQuota 调用失败?

javascript - 是否可以在 iframe 中打开弹出窗口

vue.js - 该值未绑定(bind)在 PrimeVue/TreeSelect 中

javascript - Vue root 和任何子组件之间的通信

c++ - qt c 发出文本浏览器

javascript - Twitter API 调用仅使用 javascript 获取 OAuth 的 access_token

javascript - 移动页面后会立即打开一个弹出窗口

javascript - 使用 Laravel 缓存清除

angularjs - $emit 与父子指令通信中的回调最佳方法

node.js - socket.io无法调用未定义的方法 'emit'(使用node.js和express,jade)