【问题标题】:Object data overide all previous data对象数据覆盖所有以前的数据
【发布时间】:2017-08-12 04:36:03
【问题描述】:

所以基本上我有 2 个组件,我使用 webkit simple-template 来组织我的代码,在我的项目中我有一个问题,基本上我有一个类似 textarea 的东西,每次我在那里写东西时,它应该显示一个 div那个文本,所以在我的 textarea 组件中我做了这样的事情:

<template>
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <div class="col-md-offset-3 col-md-6">
                    <label>Quote:</label>
                    <textarea v-model="quote.text" class="form-control" rows="5"></textarea>
                    <div class="text-center">
                        <button @click="addQuote" class="btn btn-primary center">Add Quote</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import { quoteBus } from '../main.js';

    export default {
        methods: {
            addQuote() {
                if (this.counter < 10) {
                    this.counter++;
                    this.quote.id = this.counter;
                    quoteBus.$emit('saveQuote', this.quote);
                }
            }
        },
        data: function () {
            return {
                quote: {},
                counter: 0
            }
        },
        created(){
            quoteBus.$on('decreaseCounter', () => {
                this.counter--
            });
        }
    }

</script>

每次我添加引号(文本)时,我都会调用另一个组件中的事件,该事件应该将对象取消移动到数组中。

组件二(带 div)

<template>
    <div class="row">
        <div class="col-md-3" v-for="(quote,$index) in quotes" @click="deleteQuote($index)">
            <div class="spacing">
                <h2>{{quote.text}}</h2>
            </div>
        </div>
    </div>
</template>

<script>
    import { quoteBus } from '../main.js';

    export default {
        data: function () {
            return {
                quotes: []
            }
        },
        methods: {
            deleteQuote(i){
                this.quotes.splice(i,1);
                quoteBus.$emit('decreaseCounter');
            }
        },
        created() {
            quoteBus.$on('saveQuote', quote => {
                this.quotes.unshift(quote);
                console.log(quote);
            });
        }
    }

</script>

正如你们所看到的,这里我有将对象添加到数组开头的 unshift,在 html 上面我做了一个 v-for 来迭代数组,一切都很好,但是显示了 div很奇怪,这是行为:

我在 textarea 上放置了“一些文本”,一切都很好,我得到了一个带有“一些文本”的 div

然后我在 textarea 上放置“一些 Text2”,并且“一些 Text2”覆盖了前一个对象,我将所有 div 与最后输入的文本一起显示,所以想象在我的第 9 个 div 中我输入“abc”all其他 8 个 div 得到“abc”,我希望你们理解这个问题。

Ps:console.log(quote) 很好地显示了我想要取消移位的对象,正是我想要传递给数组的那个对象,所以那里没有问题。

谢谢大家,

对不起,长篇大论

行情日志

先添加:

Array[1]0: Object__ob__: Observerid: 2text: "second"__proto__: Object1: Object__ob__: Observerid: 2text: "second"__proto__: Object__ob__: Observerlength: 2__proto__: Array

第二次添加:

Array[1]0: Object__ob__: Observerid: 2text: "second"proto: Object1: Object__ob__: Observerid: 2text: "second"proto: Object__ob__: Observerlength : 2__proto__: 数组

【问题讨论】:

  • 我认为所有以前的 div 仍然只绑定到一个模型。您需要进行一些克隆以绑定到新对象,这样数据就不会在所有 div 之间共享。我仍在尝试解析您的代码,我将尝试通过代码更改编写答案
  • 你能调试或控制台注销quotes数组吗?
  • 我无法搭建测试。如果您可以针对该问题创建jsfiddle,我可能会摆弄代码以进行修复。尝试按照this sample 将循环引用分解为自己的组件。可能更清楚 vue 绑定了哪些数据,以及为什么没有显示数组中的每个元素。
  • 对不起,我不在这里,我用引号中的调试更新了问题,结果在上面
  • 它只是覆盖了之前的数组,当我添加一些东西时

标签: javascript ecmascript-6 vue.js vuejs2


【解决方案1】:

问题是您需要有一个唯一标识每个元素的键。 https://vuejs.org/v2/guide/list.html#key 我建议添加这个

this.quote.id = this.counter;
this.quote.key = + new Date();//new property 

然后在你的其他组件中

<div class="col-md-3" v-for="(quote,$index) in quotes" @click="deleteQuote($index)" :key="quote.key">

不要使用您的 id 作为键,因为它会根据引号在数组中的位置而变化。

【讨论】:

  • 仍然没有解决,问题仍然不知道为什么:/
猜你喜欢
  • 1970-01-01
  • 2014-02-13
  • 2013-10-10
  • 2015-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
相关资源
最近更新 更多