无法将Vue数据传递到Vue组件

Vin*_*989 2 javascript vue.js

我似乎无法将Vue数据从内联模板传递到Vue组件.

我收到了这个错误:

vue.min.js: Uncaught TypeError: Cannot read property 'type' of undefined
Run Code Online (Sandbox Code Playgroud)

下面是我正在使用的示例代码:

Vue.component('data-item', {
  props: ['food'],
  template:"<li>{{food}}</li>"
})


var content = new Vue({
  el: '#content',
  data: {
    value: 'hello value!'
  }
})
Run Code Online (Sandbox Code Playgroud)
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>

<div id="content">
  <!-- this works
  <ol>
    <li>{{value}}</li>
  </ol>
  -->
  
  <!-- passing data from vue instance to component doesn't work -->
  <ol>
    <data-item  v-bind:food="value" inline-template></data-item>
  </ol>
  
</div>
Run Code Online (Sandbox Code Playgroud)

Dan*_*eck 6

您尝试使用inline-template而不包括内联模板. inline-templatetemplate它的DOM节点内的任何东西替换组件; 你得到的,undefined因为它是空的.

如果要使用内联模板:

Vue.component('data-item', {
  props: ['food'],
  template: "<li>This will be ignored in favor of the inline-template: {{food}}</li>"
})

var content = new Vue({
  el: '#content',
  data: {
    value: 'hello value!'
  }
})
Run Code Online (Sandbox Code Playgroud)
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div id="content">
  <ol>
    <data-item v-bind:food="value" inline-template>
      <li>I am the inline template: {{food}}</li>
    </data-item>
  </ol>
</div>
Run Code Online (Sandbox Code Playgroud)

如果你想使用data-item's'模板',只是不要包含inline-template在元素上:

Vue.component('data-item', {
  props: ['food'],
  template:"<li>I am the component template: {{food}}</li>"
})

var content = new Vue({
  el: '#content',
  data: {
    value: 'hello value!'
  }
})
Run Code Online (Sandbox Code Playgroud)
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div id="content">
  <ol>
    <data-item v-bind:food="value">
      I am the contents of the DOM node, which will be
      replaced by data-item's template.
    </data-item>
  </ol>
</div>
Run Code Online (Sandbox Code Playgroud)