Is this the right way to modify props in the Vue.js component?

Props unidirectional data flow is emphasized in

Vue.js. Do not modify the props directly in the subcomponents. However, when I was writing, I found that the following modification is normal and can be run

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>
<body>
<template id="demo">
    <div>
        <ul>
            <li v-for="item in info.list">{{ item }}</li>
        </ul>
        <button @click="addItem">AddItem</button>
    </div>
</template>

<div id="app">
    <v-demo :info="info"></v-demo>
</div>

<script type="text/javascript">
    Vue.component("v-demo", {
        template: "-sharpdemo",
        props: ["info"],
        methods: {
            addItem: function () {
                this.info.list.push(1);
                // this.info = {
                //     "list": [1,2,3,4,5,6]
                // };
            }
        }
    })
    var vn = new Vue({
        el: "-sharpapp",
        data: {
            "info": {
                "list": [1,2,3]
            }
        }
    })
</script>
</body>
</html>

when I directly this.info =? When , Vue warns not to modify the value of props directly. However, the way I push above can be regarded as changing the value of props, right? Is this good practice?

Mar.29,2022

js can be divided into data types and reference types, the basic type is a direct assignment, and the value of the reference type is a pointer to an address. Understand these, let's take a look at your props things: this.info =? You have changed the reference pointer to a value, and it is obvious that vue can be checked, so it prompts you that props should not be tampered with in the subcomponents. This.info.list.push (1): list is a complex data type, the gods do not know where you should be, because the value of all places is just a pointer to this list, how can you list, the pointer will not change (as for Obeject.defineProperty () can not know the array change is not mentioned here), but why the corresponding view will change? Because you use the vue mutation method push, a thing that will actively trigger watch and update the view (not explained in detail). Back to the question: is this a good practice? Absolutely not. In order to facilitate data flow tracking (people: you and your pot catcher can see what you wrote later), the idea of vue is one-way data flow, that is, you want to change the value from props, use $emit to tell the father component to change it in father, so that you can achieve one-way data flow

Menu