template or render function not defined.

下午寫 Vue $parent 實例時,總是遇到這個問題,不禁讓人陷入沉思


1.gif

Vue,醒醒啊,你怎么了,貼上子組件源代碼

<template>
  <div>
    <child-component1></child-component1>
    <child-component2></child-component2>
    <button v-on:click="showChildComponentData">顯示子組件的數(shù)據(jù)</button>
  </div>
</template>

<script>
export default{
  components: {
    'child-component1': {
      template: '#child-component1',
      data: function () {
        return {
          msg: 'child component 111111'
        }
      }
    },
    'child-component2': {
      template: '#child-component2',
      data: function () {
        return {
          msg: 'child component 222222'
        }
      }
    }
  },
  methods: {
    showChildComponentData: function () {
      for (var i = 0; i < this.$children.length; i++) {
        alert(this.$children[i].msg)
      }
    }
  }
}
</script>

這是報錯信息:


1.png

修改后的代碼:

<template>
  <div>
    <child-component1></child-component1>
    <child-component2></child-component2>
    <button v-on:click="showChildComponentData">顯示子組件的數(shù)據(jù)</button>
  </div>
</template>

<script>
export default{
  components: {
    'child-component1': {
      template: '<p>I am ok</p>',
      data: function () {
        return {
          msg: 'child component 111111'
        }
      }
    },
    'child-component2': {
      template: '<p>ok</p>',
      data: function () {
        return {
          msg: 'child component 222222'
        }
      }
    }
  },
  methods: {
    showChildComponentData: function () {
      for (var i = 0; i < this.$children.length; i++) {
        alert(this.$children[i].msg)
      }
    }
  }
}
</script>

這樣就好了,原因是 Vue 的構(gòu)建,運行時構(gòu)建刪除了模板編譯的功能,因此無法支持帶 template 屬性的 Vue 實例選項。

總結(jié):每個子組件的 template 屬性值不一樣。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容