当我设置一个v-for时,props未定义。

huangapple go评论66阅读模式
英文:

When i set a v-for, props undefined

问题

I'm doing a website of a League of Legends assist, and i have a component with five elements (one for each line). I had it setted without a v-for, and it worked perfect, but when i configured it with a v-for for future eficiency, it gives me a error of not getting the prop passed by the father.

If i remove the v-for it works perfectly.

Here is the code without the v-for:

<template>
    <div class="champsIconsSelection">
      <button class="champIconSelection" :class="{[lado]: true, picked: (this.rolePicked==='TOP'), setted: (this.champsPicked[0] !== 0)}" @click="pickRole('TOP')">
        <img :src="'/static/icons/'+ this.champsPicked[0] +'.jpg'" v-if="this.champsPicked[0] !== 0">
      </button>
      <button class="champIconSelection" :class="{[lado]: true, picked: (this.rolePicked==='JGL'), setted: (this.champsPicked[1] !== 0)}" @click="pickRole('JGL')">
        <img :src="'/static/icons/'+ this.champsPicked[1] +'.jpg'" v-if="this.champsPicked[1] !== 0">
      </button>
      <button class="champIconSelection" :class="{[lado]: true, picked: (this.rolePicked==='MID'), setted: (this.champsPicked[2] !== 0)}" @click="pickRole('MID')">
        <img :src="'/static/icons/'+ this.champsPicked[2] +'.jpg'" v-if="this.champsPicked[2] !== 0">
      </button>
      <button class="champIconSelection" :class="{[lado]: true, picked: (this.rolePicked==='ADC'), setted: (this.champsPicked[3] !== 0)}" @click="pickRole('ADC')">
        <img :src="'/static/icons/'+ this.champsPicked[3] +'.jpg'" v-if="this.champsPicked[3] !== 0">
      </button>
      <button class="champIconSelection" :class="{[lado]: true, picked: (this.rolePicked==='SUP'), setted: (this.champsPicked[4] !== 0)}" @click="pickRole('SUP')">
        <img :src="'/static/icons/'+ this.champsPicked[4] +'.jpg'" v-if="this.champsPicked[4] !== 0">
      </button>
    </div>
</template>

And here is with the v-for:

<template>
    <div class="champsIconsSelection">
      <div v-for="position in positions" v-bind:key="position.pos">
        <button class="champIconSelection"
                :class="{[lado]: true, picked: (this.rolePicked && this.rolePicked===position.role), setted: (this.champsPicked[position.pos] !== 0)}"
                @click="pickRole(position.role)">
          <img :src="'/static/icons/'+ this.champsPicked[position.pos] +'.jpg'" v-if="this.champsPicked[position.pos] !== 0">
        </button>
      </div>
    </div>
</template>

The rest of the component, in case it's useful:

<script>
export default {
  name: 'champ_selector',
  props: ['lado', 'rolePicked', 'champsPicked'],
  data () {
    return {
      positions: [
        {
          'pos': 0,
          'role': 'TOP'
        },
        {
          'pos': 1,
          'role': 'JGL'
        },
        {
          'pos': 2,
          'role': 'MID'
        },
        {
          'pos': 3,
          'role': 'ADC'
        },
        {
          'pos': 4,
          'role': 'SUP'
        }
      ]
    }
  },
  methods: {
    pickRole (role) {
      this.$emit('pickRole', role)
    }
  }
}
</script>

This is the parent component:

<template>
  <main>
    <div id="champZone">
      <div id="blueChampSelect">
        <champSelect :lado="'blue'" :rolePicked="b_rolePicked" :champsPicked="b_champsPicked" @pickRole="pickRoleB"></champSelect>
      </div>
      <div id="champList">
        <champList @setChamp="setChamp"></champList>
      </div>
      <div id="redChampSelect">
        <champSelect :lado="'red'" :rolePicked="r_rolePicked" :champsPicked="r_champsPicked" @pickRole="pickRoleR"></champSelect>
      </div>
    </div>

    <dataZone></dataZone>
  </main>
</template>

<script>
import champList from './ChampList.vue'
import champSelect from './ChampSelect.vue'
import dataZone from './DataZone.vue'

export default {
  data () {
    return {
      b_rolePicked: '',
      r_rolePicked: '',
      b_champsPicked: [0, 0, 0, 0, 0],
      r_champsPicked: [0, 0, 0, 0, 0],
      roleDict: {'TOP': 0, 'JGL': 1, 'MID': 2, 'ADC': 3, 'SUP': 4}
    }
  },
  components: {
    champList,
    champSelect,
    dataZone
  }
}
</script>

I tried setting something in the variable passed throught the 'rolePicked' prop, but it didn't work either. It feels weird.

英文:

I'm doing a website of a League of Legends assist, and i have a component with five elements (one for each line). I had it setted without a v-for, and it worked perfect, but when i configured it with a v-for for future eficiency, it gives me a error of not getting the prop passed by the father.

If i remove the v-for it works perfectly.

Here is the code without the v-for:

&lt;template&gt;
    &lt;div class=&quot;champsIconsSelection&quot;&gt;
      &lt;button class=&quot;champIconSelection&quot; :class=&quot;{[lado]: true, picked: (this.rolePicked===&#39;TOP&#39;), setted: (this.champsPicked[0] !== 0)}&quot; @click=&quot;pickRole(&#39;TOP&#39;)&quot;&gt;
        &lt;img :src=&quot;&#39;/static/icons/&#39;+ this.champsPicked[0] +&#39;.jpg&#39;&quot; v-if=&quot;this.champsPicked[0] !== 0&quot;&gt;
      &lt;/button&gt;
      &lt;button class=&quot;champIconSelection&quot; :class=&quot;{[lado]: true, picked: (this.rolePicked===&#39;JGL&#39;), setted: (this.champsPicked[1] !== 0)}&quot; @click=&quot;pickRole(&#39;JGL&#39;)&quot;&gt;
        &lt;img :src=&quot;&#39;/static/icons/&#39;+ this.champsPicked[1] +&#39;.jpg&#39;&quot; v-if=&quot;this.champsPicked[1] !== 0&quot;&gt;
      &lt;/button&gt;
      &lt;button class=&quot;champIconSelection&quot; :class=&quot;{[lado]: true, picked: (this.rolePicked===&#39;MID&#39;), setted: (this.champsPicked[2] !== 0)}&quot; @click=&quot;pickRole(&#39;MID&#39;)&quot;&gt;
        &lt;img :src=&quot;&#39;/static/icons/&#39;+ this.champsPicked[2] +&#39;.jpg&#39;&quot; v-if=&quot;this.champsPicked[2] !== 0&quot;&gt;
      &lt;/button&gt;
      &lt;button class=&quot;champIconSelection&quot; :class=&quot;{[lado]: true, picked: (this.rolePicked===&#39;ADC&#39;), setted: (this.champsPicked[3] !== 0)}&quot; @click=&quot;pickRole(&#39;ADC&#39;)&quot;&gt;
        &lt;img :src=&quot;&#39;/static/icons/&#39;+ this.champsPicked[3] +&#39;.jpg&#39;&quot; v-if=&quot;this.champsPicked[3] !== 0&quot;&gt;
      &lt;/button&gt;
      &lt;button class=&quot;champIconSelection&quot; :class=&quot;{[lado]: true, picked: (this.rolePicked===&#39;SUP&#39;), setted: (this.champsPicked[4] !== 0)}&quot; @click=&quot;pickRole(&#39;SUP&#39;)&quot;&gt;
        &lt;img :src=&quot;&#39;/static/icons/&#39;+ this.champsPicked[4] +&#39;.jpg&#39;&quot; v-if=&quot;this.champsPicked[4] !== 0&quot;&gt;
      &lt;/button&gt;
    &lt;/div&gt;
&lt;/template&gt;

And here is with the v-for:

&lt;template&gt;
    &lt;div class=&quot;champsIconsSelection&quot;&gt;
      &lt;div v-for=&quot;position in positions&quot; v-bind:key=&quot;position.pos&quot;&gt;
        &lt;button class=&quot;champIconSelection&quot;
                :class=&quot;{[lado]: true, picked: (this.rolePicked &amp;&amp; this.rolePicked===position.role), setted: (this.champsPicked[position.pos] !== 0)}&quot;
                @click=&quot;pickRole(position.role)&quot;&gt;
          &lt;img :src=&quot;&#39;/static/icons/&#39;+ this.champsPicked[position.pos] +&#39;.jpg&#39;&quot; v-if=&quot;this.champsPicked[position.pos] !== 0&quot;&gt;
        &lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/template&gt;

The rest of the component, in case it's useful:

&lt;script&gt;
export default {
  name: &#39;champ_selector&#39;,
  props: [&#39;lado&#39;, &#39;rolePicked&#39;, &#39;champsPicked&#39;],
  data () {
    return {
      positions: [
        {
          &#39;pos&#39;: 0,
          &#39;role&#39;: &#39;TOP&#39;
        },
        {
          &#39;pos&#39;: 1,
          &#39;role&#39;: &#39;JGL&#39;
        },
        {
          &#39;pos&#39;: 2,
          &#39;role&#39;: &#39;MID&#39;
        },
        {
          &#39;pos&#39;: 3,
          &#39;role&#39;: &#39;ADC&#39;
        },
        {
          &#39;pos&#39;: 4,
          &#39;role&#39;: &#39;SUP&#39;
        }
      ]
    }
  },
  methods: {
    pickRole (role) {
      this.$emit(&#39;pickRole&#39;, role)
    }
  }
}
&lt;/script&gt;

This is the parent component:

&lt;template&gt;
  &lt;main&gt;
    &lt;div id=&quot;champZone&quot;&gt;
      &lt;div id=&quot;blueChampSelect&quot;&gt;
        &lt;champSelect :lado=&quot;&#39;blue&#39;&quot; :rolePicked=&quot;b_rolePicked&quot; :champsPicked=&quot;b_champsPicked&quot; @pickRole=&quot;pickRoleB&quot;&gt;&lt;/champSelect&gt;
      &lt;/div&gt;
      &lt;div id=&quot;champList&quot;&gt;
        &lt;champList @setChamp=&quot;setChamp&quot;&gt;&lt;/champList&gt;
      &lt;/div&gt;
      &lt;div id=&quot;redChampSelect&quot;&gt;
        &lt;champSelect :lado=&quot;&#39;red&#39;&quot; :rolePicked=&quot;r_rolePicked&quot; :champsPicked=&quot;r_champsPicked&quot; @pickRole=&quot;pickRoleR&quot;&gt;&lt;/champSelect&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;dataZone&gt;&lt;/dataZone&gt;
  &lt;/main&gt;
&lt;/template&gt;

&lt;script&gt;
import champList from &#39;./ChampList.vue&#39;
import champSelect from &#39;./ChampSelect.vue&#39;
import dataZone from &#39;./DataZone.vue&#39;

export default {
  data () {
    return {
      b_rolePicked: &#39;&#39;,
      r_rolePicked: &#39;&#39;,
      b_champsPicked: [0, 0, 0, 0, 0],
      r_champsPicked: [0, 0, 0, 0, 0],
      roleDict: {&#39;TOP&#39;: 0, &#39;JGL&#39;: 1, &#39;MID&#39;: 2, &#39;ADC&#39;: 3, &#39;SUP&#39;: 4}
    }
  },
  components: {
    champList,
    champSelect,
    dataZone
  }
}
&lt;/script&gt;

I tried setting something in the variable passed throught the 'rolePicked' prop, but it didn't work either.
It feels weird.

答案1

得分: 1

You seem to be assuming the this context within the markup. Try changing this.

<button class="champIconSelection"
        :class="{[lado]: true, picked: (rolePicked && rolePicked===position.role), setted: (champsPicked[position.pos] !== 0)}"
        @click="pickRole(position.role)">
  <img :src="'/static/icons/' + champsPicked[position.pos] + '.jpg'" v-if="champsPicked[position.pos] !== 0">
</button>

Notice that I removed the this. prefix you added to the props.

英文:

You seem to be assuming the this context within the markup. Try changing this.

&lt;button class=&quot;champIconSelection&quot;
                :class=&quot;{[lado]: true, picked: (this.rolePicked &amp;&amp; this.rolePicked===position.role), setted: (this.champsPicked[position.pos] !== 0)}&quot;
                @click=&quot;pickRole(position.role)&quot;&gt;
          &lt;img :src=&quot;&#39;/static/icons/&#39;+ this.champsPicked[position.pos] +&#39;.jpg&#39;&quot; v-if=&quot;this.champsPicked[position.pos] !== 0&quot;&gt;
        &lt;/button&gt;

to this.

&lt;button class=&quot;champIconSelection&quot;
                :class=&quot;{[lado]: true, picked: (rolePicked &amp;&amp; rolePicked===position.role), setted: (champsPicked[position.pos] !== 0)}&quot;
                @click=&quot;pickRole(position.role)&quot;&gt;
          &lt;img :src=&quot;&#39;/static/icons/&#39;+ champsPicked[position.pos] +&#39;.jpg&#39;&quot; v-if=&quot;champsPicked[position.pos] !== 0&quot;&gt;
        &lt;/button&gt;

Notice that I removed the this. prefix you added to the props.

huangapple
  • 本文由 发表于 2023年6月8日 17:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定