简体   繁体   English

Vue.js vuex道具不会重新渲染

[英]Vue.js vuex props don't rerender

I'm using Vuex and display data like this: 我正在使用Vuex并显示如下数据:

<template>
    <div>
        <div>
            <div v-for="port in ports">
                <port :port="port"></port>
            </div>
        </div>
    </div>
</template>

<script>
    import { mapState } from 'vuex';

    export default {
        computed: {
            ...mapState({
                ports: state => state.ports.ports,
            })
        }
    }
</script>

The problem that I have now is that when I change the ports state in another component it updates the ports state correctly but the port in the v-for loop is not updated. 我现在遇到的问题是,当我更改另一个组件中的ports state ,它会正确更新ports state ,但是v-for循环中的端口未更新。

How do I make sure the data in the port component rerenders correctly as well? 如何确保端口组件中的数据也正确重新rerenders

Vue Beginner Gotchas: Why isn't the DOM updating? Vue初学者陷阱:为何不更新DOM?

Most of the time, when you change a Vue instance's data, the view updates. 大多数情况下,当您更改Vue实例的数据时,视图会更新。 But there are two edge cases: 但是有两种情况:

  1. When you are adding a new property that wasn't present when the data was observed. 当您添加观察数据时不存在的新属性时。

  2. When you modify an Array by directly setting an index (eg arr[0] = val) or modifying its length property. 通过直接设置索引(例如,arr [0] = val)或修改其length属性来修改Array时。

If you are changing your ports data by doing something like this: 如果要通过以下操作更改端口数据:

this.ports[0] = 1234;

Or something like this: 或类似这样的东西:

this.ports[4].property = 'some value';

Then Vue won't be able to detect that a change occurred. 这样,Vue将无法检测到发生了更改。 You can get around this pitfall by doing: 您可以通过以下方法解决此陷阱:

Vue.set(this.ports, 0, 1234);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM