代码之家  ›  专栏  ›  技术社区  ›  Yamaha32088

VueJS v-for内部组件模板似乎没有循环

  •  0
  • Yamaha32088  · 技术社区  · 6 年前

    CodePen

    <div id="sentimentForm">
      <customer-sentiment :sentiment-types="sentimentTypes" sentiment-selected="neutral"></customer-sentiment>
    </div>
    
    var sentimentReasons = [
      {label: 'Happy', value: '3', Display: 'Happy', Key: 'happy'},
      {label: 'Neutral', value: '2', Display: 'Neutral', Key: 'neutral'},
      {label: 'Angry', value: '1', Display: 'Angry', Key: 'angry'}
    ];
    
    Vue.component('v-select', VueSelect.VueSelect);
    
    Vue.component('customer-sentiment', {
      props: {
        sentiment: {
          default: null
        }, 
        sentimentSelected: {
          default: null
        },
        sentimentTypes: {
          type: Array,
          default() {
            return []
          },
        }
      },
      template: `
      <div v-for="(item, index) in mutableOptions">
        <h3>{{ index }}<h3>
        <h4>{{ item.Display }}<h4>
      </div>`,
      created: function() {
        console.log(this.sentimentTypes)
        this.mutableOptions = this.sentimentTypes;
      },
      data() {
        return {
          mutableOptions: []
        }
      }  
    });
    
    var app = new Vue({
      el: '#sentimentForm',
      data: function() {
        return {
          sentiment: '',
          sentimentSelected: '',
          sentimentTypes: sentimentReasons
        }
      }
    });
    
    1 回复  |  直到 6 年前
        1
  •  4
  •   clueless_anh    6 年前

    这个 customer-sentiment <div> 在第一级(从 v-for 循环),所以将当前模板嵌套在 div 应该能解决问题。

    template: `
      <div>
        <div v-for="(item, index) in mutableOptions">
          <h3>{{ index }}<h3>
          <h4>{{ item.Display }}<h4>
        </div>
      </div>`,
    created: function() {
      console.log(this.sentimentTypes)
      this.mutableOptions = this.sentimentTypes;
    },