本文主要介绍一下组件对props(即传入参数)的处理。

props是父组件给子组件传递的参数,而子组件需要修改props的场景非常多,但props并不像组件自己的属性那样可以随意修改(因为props会同时影响父组件和子组件),props的修改必须遵循以下规则:

  1. 子组件不能直接修改props;
  2. 如果props是对象,子组件可以修改对象的属性值(包括增加删除属性);
  3. 如果props是数组,子组件可以修改数组的元素(包括增加删除元素),如果元素是对象,还可以修改元素的属性值(包括增加删除元素属性)。

下面上示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>props</title>
    <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
</head>
<body>
<div id="app">
    <div>
        <button type="button" @click="show">container</button>
    </div>
    <container :parama="paramA" :paramb="paramB" :paramc="paramC" :paramd="paramD"></container>
</div>
<script>
    Vue.component('container', {
        template: '<div><br><button type="button" @click="changeA">change paramA</button>' +
        '<br><br><button type="button" @click="changeB">change paramB</button>' +
        '<br><br><button type="button" @click="changeC">change paramC</button>' +
        '<br><br><button type="button" @click="changeD">change paramD</button></div>',
        props: ["parama", "paramb", "paramc", "paramd"],
        methods: {
            changeA: function () {
                //触发时会报错
                this.parama = "paramA changed";
            },
            changeB: function () {
                this.paramb.key = "key changed";
                this.paramb.value = "value changed";
                //上下两种写法是等价的
                // this.$set(this.paramb, "key", "key changed");
                // this.$set(this.paramb, "value", "value changed");
                this.paramb.add = "new attr";
                delete this.paramb.removeAttr;
            },
            changeC: function () {
                this.paramc.push(4);
                this.paramc[0] = -1;
                //上下两种写法是等价的
                // this.$set(this.paramc, 0, -1);
            },
            changeD: function () {
                this.paramd.push({
                    lon: 40,
                    lat: 70
                });
                this.paramd[0] = {
                    lon: 20,
                    lat: 50
                };
                //上下两种写法是等价的
                // this.$set(this.paramd, 0, {
                //     lon: 20,
                //     lat: 50
                // });
            }
        }
    });
    new Vue({
        el: '#app',
        data: {
            paramA: "paramA",
            paramB: {
                key: "it is key",
                value: "it is value",
                removeAttr: "it is remove"
            },
            paramC: [1, 2, 3],
            paramD: [
                {
                    lon: 30,
                    lat: 60
                }
            ]
        },
        methods: {
            show: function () {
                console.log("paramA is " + this.paramA);
                console.log("paramB.key is " + this.paramB.key);
                console.log("paramB.value is " + this.paramB.value);
                if (this.paramB.add) {
                    console.log("paramB.add is " + this.paramB.add);
                }
                if (this.paramB.removeAttr) {
                    console.log("paramB.removeAttr is " + this.paramB.removeAttr);
                }
                console.log("paramC.length is " + this.paramC.length);
                console.log("paramC[0] is " + this.paramC[0]);
                console.log("paramD.length is " + this.paramD.length);
                console.log("paramD[0].lon is " + this.paramD[0].lon);
                console.log("paramD[0].lat is " + this.paramD[0].lat);
                if (this.paramD.length === 2) {
                    console.log("paramD[1].lon is " + this.paramD[1].lon);
                    console.log("paramD[1].lat is " + this.paramD[1].lat);
                }
            }
        }
    });
</script>
</body>
</html>

当尝试直接修改props时(对应上例为点击"change paramA"按钮),控制台会报错:
在这里插入图片描述
当执行其他修改动作时(对应上例为点击除"change paramA"外的其他按钮),控制台没有报错,然后在父组件中查看props是否成功修改(点击"container"按钮),可以看到父组件传入的props都修改成功了:
在这里插入图片描述

Logo

前往低代码交流专区

更多推荐