美文网首页
Vue学习笔记(2):Vue使用中碰到的一些坑

Vue学习笔记(2):Vue使用中碰到的一些坑

作者: 疏花 | 来源:发表于2018-08-15 11:22 被阅读65次

    1.子组件获取不到父组件异步加载的值
    父组件:

     <template>
        <child :info="childInfo"></child>
     </template>
    <script>
    export default {
      components: {
        Child
      },
      data: function () {
        return {
          childInfo:'1'
      }
      },
      created () {
        this.getInfo()
      },
      mounted () {},
      methods: {
        getGoodInfo: function () {
          this.$axios
            .get(
              'http://localhost:8000/getInfo'
            )
            .then(response => {
              this.getInfo= response.data.info
            })
        }
      }
    </script>
    

    由于数据是异步获取的,而子组件在一开始就渲染了,所以这时候info是获取不到数据的,解决方法:

    <child v-if="childInfo != '1' " :info="childInfo"></child>
    

    用v-if加个判断,当有值时才获取。

    相关文章

      网友评论

          本文标题:Vue学习笔记(2):Vue使用中碰到的一些坑

          本文链接:https://www.haomeiwen.com/subject/voiibftx.html