下述组件传值指引用类型(数组或对象)传值。

准备:单向数据流

所有的 prop 都使得其父子 prop 之间形成了一个单向下行绑定:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。这样会防止从子组件意外变更父级组件的状态,从而导致你的应用的数据流向难以理解。

额外的,每次父级组件发生变更时,子组件中所有的 prop 都将会刷新为最新的值。这意味着你应该在一个子组件内部改变 prop。如果你这样做了,Vue 会在浏览器的控制台中发出警告。

  • 这个 prop 用来传递一个初始值;这个子组件接下来希望将其作为一个本地的 prop 数据来使用。
    定义一个本地的 data property 并将这个 prop 用作其初始值
    1. props: ['initialCounter'],
    2. data: function () {
    3. return {
    4. counter: this.initialCounter
    5. }
    6. }
  • 这个 prop 以一种原始的值传入且需要进行转换。
    使用这个 prop 的值来定义一个计算属性
    1. props: ['size'],
    2. computed: {
    3. normalizedSize: function () {
    4. return this.size.trim().toLowerCase()
    5. }
    6. }

注意在 JavaScript 中对象和数组是通过引用传入的,所以对于一个数组或对象类型的 prop 来说,在子组件中改变变更这个对象或数组本身将会影响到父组件的状态。

问题

父子组件间,通过引用类型传值,为什么控制台不会告警(基本类型会告警)?Object.assign() 或者 JSON.parse(JSON.stringify()) 是在子组件中传引用值的标准处理方法吗?

问题1

父组件 App.vue

  1. <template>
  2. <div id="app">
  3. <child :initialValue="valueEmit">
  4. </child>
  5. </div>
  6. </template>
  7. <script>
  8. import childEmit from './components/child.vue'
  9. export default {
  10. data () {
  11. return {
  12. valueEmit: {dog: 1, cat: 2}
  13. }
  14. },
  15. components: {
  16. child
  17. }
  18. }
  19. </script>

子组件 components/child.vue

  1. <template>
  2. <div class="child-container">
  3. <p>
  4. <label for="cat">猫🐱:</label>
  5. <input id="cat" type="text" v-model="value.cat" />
  6. </p>
  7. <p>
  8. <label for="dog">狗🐶:</label>
  9. <input id="dog" type="text" v-model="value.dog" />
  10. </p>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'child',
  16. props: {
  17. initialValue: Object
  18. },
  19. data () {
  20. return {
  21. value = this.initialValue
  22. }
  23. }
  24. }
  25. </script>

子组件内的修改,父组件也会直接变更,且不告警!

问题2

修改子组件,子组件 components/child.vue

  1. export default {
  2. name: 'child',
  3. props: {
  4. initialValue: Object
  5. },
  6. data () {
  7. return {
  8. value: Object.assign({}, this.initialValue)
  9. }
  10. }
  11. }

切断了引用,但是父组件变化不会触发子组件响应!(computed or watch 可以实现)

问题3

父组件 App.vue

  1. <template>
  2. <div id="app">
  3. <child
  4. :initialValue="valueEmit"
  5. @update-value-by-child="updateParentValue">
  6. </child>
  7. </div>
  8. </template>
  9. <script>
  10. import childEmit from './components/child.vue'
  11. export default {
  12. data () {
  13. return {
  14. valueEmit: {dog: 1, cat: 2}
  15. }
  16. },
  17. components: {
  18. child
  19. },
  20. methods: {
  21. updateParentValue(newValue) {
  22. this.valueEmit = newValue
  23. }
  24. }
  25. }
  26. </script>

子组件 components/child.vue

  1. <template>
  2. <div class="child-container">
  3. <p>
  4. <label for="cat">猫🐱:</label>
  5. <input id="cat" type="text" v-model="valueEmit.cat"
  6. @input="$emit('update-value-by-child', valueEmit)" />
  7. </p>
  8. <p>
  9. <label for="dog">狗🐶:</label>
  10. <input id="dog" type="text" v-model="valueEmit.dog" />
  11. </p>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'child',
  17. props: {
  18. initialValue: Object
  19. },
  20. data() {
  21. return {
  22. valueEmit: Object.assign({}, this.initialValue)
  23. }
  24. }
  25. }
  26. </script>

现象:

  1. 首先对“dog”进行修改,父组件的 initialValue 并未发生改变
  2. 对“cat”进行修改,父组件的 initialValue 发生变化(dog、cat都被修改了)
  3. 此时,在对“dog”修改,父组件的 initialValue 发生变化!

总结

  1. 纯展示
    直接使用父组件属性,不会有副作用!
    1. <template>
    2. <div>
    3. {{parentObj.value}}
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. name: 'child',
    9. props: {
    10. parentObj: Object
    11. }
    12. }
    13. </script>
  1. 只子组件内部修改,父组件不会修改(即,父组件只做初始化)
    子组件 data 中声明新的数据,通过 Object.assign() 或者 JSON.parse(JSON.stringify()) 切断引用即可。
    1. <template>
    2. <div>
    3. <input type="text" v-model="childObj.value">
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. name: 'child',
    9. props: {
    10. parentObj: Object
    11. },
    12. data () {
    13. return {
    14. childObj: Object.assign({}, this.parentObj)
    15. }
    16. }
    17. }
    18. </script>
  1. 父子组件都会修改
    通过 computed 或者 watch 进行处理
    1. <template>
    2. <div>
    3. <input type="text" v-model="childObj.value">
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. name: 'child',
    9. props: {
    10. parentObj: Object
    11. },
    12. computed: {
    13. childObj () {
    14. return Object.assign({}, this.parentObj)
    15. }
    16. }
    17. }
    18. </script>


或者 watch 方式

  1. export default {
  2. name: 'child',
  3. props: {
  4. parentObj: Object
  5. },
  6. data () {
  7. return {
  8. childObj: {}
  9. }
  10. },
  11. watch: {
  12. parentObj: {
  13. handler (val, oldVal) {
  14. if (val) {
  15. this.childObj = Object.assign({}, this.parentObj)
  16. }
  17. },
  18. deep: true,
  19. immediate: true
  20. }
  21. }
  22. }


关于 watch 和 computed 区别: https://ligang.blog.csdn.net/article/details/94590314
关于 watch 的两个问题

  1. 在触发 ”提交/查询“ 按钮之前,可能就被刷新了
  2. 父页面刷新(tag-view)怎么通知子组件刷新,refs?(组件A => 组件B => 组件C)