日常开发记录--父子组件通信,当父组件传的值为null或undefined时报错

父组件About.vue
<template>
  <div class="about">
    <h1>Today is not easy,</h1>
    <h1>Tomorrow is more difficult,</h1>
    <h1>But the day after tomorrow will be wonderful!</h1>
    <Child :info="info || {}"/>
  </div>
</template>

<script>
import Child from './components/Child.vue'
export default {
  components: { Child },
  data () {
    return {
      // 当info传的是null或者undefined时,控制台会报错,传空对象倒不会出错
      info: null
    }
  }
}
</script>

子组件Child.vue

<template>
  <div>{{ info.school }}</div>
</template>

<script>
export default {
  name: 'Child',
  props: {
    info: {
      type: Object,
      required: true
    }
  },
  data () {
    return {

    }
  }
}
</script>

<style>

</style>

 

posted on 2022-05-30 15:47  法老的微笑  阅读(224)  评论(0)    收藏  举报