vue prop属性传值与传引用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012424148/article/details/82587197

vue组件在prop里根据type决定传值还是传引用。

简要如下:

传值:String、Number、Boolean

传引用:Array、Object

若想将数组或对象类型也以值形式传递怎么办呢?如下方式可以实现:

// component-A 引用component-B组件
<component-B :person="personList" 
             :personBak="person_Bak">
</component-B>


// component-A 部分关键代码

// 将数组复制,personBak与personList是两个“内容”相同但地址不一样的对象(数组),
// 这样可以变相的实现“传值”,person或personBak互不影响
person_Bak = JSON.parse(JSON.stringfy(this.personList));    


//component-B props部分
props: {
    person: {
        type: Object,
        default: {}
    },
    personBak: {
        type: Object,
        default: {}
    }
}

猜你喜欢

转载自blog.csdn.net/u012424148/article/details/82587197