3 回答

TA貢獻1155條經(jīng)驗 獲得超0個贊
你想使用 v-for 循環(huán)來渲染 div,并寫一次條件:
模板:
<template v-for="(value, index) in fetch">
<div v-if="value > 0" >
<p> DIV {{ index + 1 }} </p>
</div>
</template>
如果要使用數(shù)組的一部分,從 X 索引開始到 X+Y 索引結(jié)束,請使用 array.splice 并迭代新數(shù)組(從索引 1 開始并獲取以下 3 個索引):
let filteredFetch = fetch.splice(1, 4);
模板:
<template v-for="(value, index) in filteredFetch">
<div v-if="value > 0" >
<p> DIV {{ index + 1 }} </p>
</div>
</template>

TA貢獻1859條經(jīng)驗 獲得超6個贊
解耦的方法是控制數(shù)據(jù)——在這種情況下不要弄亂v-ifs,使用計算屬性(或方法,如果你需要比這更復雜的話):
new Vue({
el: "#app",
data: {
nextNumber: null,
fetch: []
},
computed: {
filteredFetch3() {
// the v-if is solved here with a filter
return this.fetch.filter(e => e > 3)
},
filteredFetch5() {
// the v-if is solved here with a filter
return this.fetch.filter(e => e > 5)
}
},
methods: {
pushNumber() {
this.fetch.push(Number(this.nextNumber))
this.nextNumber = null
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<label for="numberInput">
Number input: <input id="numberInput" v-model="nextNumber" /><button @click="pushNumber">ADD NUMBER TO FETCH LIST</button>
</label>
<hr /> FETCH (ALL THE FETCHED NUMBERS APPEAR HERE):
<br /> {{ fetch }}
<hr /> ONLY NUMBERS ABOVE 3 WILL APPEAR IN THIS LIST:
<ul>
<li v-for="(number, i) in filteredFetch3" :key="`${ number }-${i}`">
{{ number }}
</li>
</ul>
<hr /> ONLY NUMBERS ABOVE 5 WILL APPEAR IN THIS LIST:
<ul>
<li v-for="(number, j) in filteredFetch5" :key="`${ number }-${j}`">
{{ number }}
</li>
</ul>
</div>

TA貢獻1886條經(jīng)驗 獲得超2個贊
我不知道這是你想要實現(xiàn)的,但這是我的理解,請檢查 jsfiddle 中的這段代碼以及它的外觀:
new Vue({
el: "#app",
mounted(){
var totalBoxes = 10;
for(let b=0; b < totalBoxes; b++){
var replyDataObj1 = b;
replyDataObj1={
"route_id" : b
}
this.toListFetch= replyDataObj1; /** this will collect all the data from fetch as a list **/
this.fetch.push(this.toListFetch); /** list from toListFetch will be pushed to this.fetch **/
}
},
data() {
return {
fetch:[],
toListFetch: ''
}
},
computed: {
filteredBoxes() {
// Computed property to return only fecth where route_id is greater than 0
return this.fetch.filter(f => f["route_id"] > 0);
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<p>
Filtered boxes
</p>
<div v-for="(f, indexFiltered) in filteredBoxes" :key="indexFiltered">
DIV {{ f.route_id }}
</div>
</div>
計算屬性非常適合從數(shù)據(jù)屬性中的項目中查找或過濾
添加回答
舉報