我想按名称返回一组唯一的卡片。我可以使用以下代码在Vuejs中做到这一点。
<template>
<div id='Home'>
<div class="columns is-multiline">
<card v-for="card in uniqueCards" :card="card" :key="card.id"/>
</div>
</div>
</template>
<script>
import uniqBy from 'lodash/uniqBy'
import Card from '@/components/Site/Card'
export default {
name: 'Home',
components: { Card },
computed: {
cards () {
return this.$store.state.cards
},
uniqueCards () {
return uniqBy(this.$store.state.cards, function (r) {
return r.project
})
}
}
}
</script>
我正在将我的项目转换为Nuxtjs,并使用api获取appCards。如何使用axios实现与上述相同的功能?下面的作品,但我想使用uniqueCards()函数。我在这里做什么错了?
<template>
<div id="Home">
<div class="columns is-multiline">
<card v-for="card in appCards" :card="card" :key="card.id"/>
</div>
</div>
</template>
<script>
import uniqBy from 'lodash/uniqBy'
import Card from "~/components/Card.vue";
export default {
components: {
Card
},
data() {
return {
appCards: []
};
},
async asyncData({ $axios, params }) {
try {
let appCards = await $axios.$get(`/appcards/`);
return { appCards };
} catch (e) {
return { appCards: [] };
}
},
mounted: {
uniqueCards () {
uniqBy(this.appCards, function (r) {
return r.project;
})
}
},
};
</script>
答案 0 :(得分:0)
没关系,让它起作用!我忘记了正确的语法。
<template>
<div id="Home">
<section class="section is-small">
<div class="container">
<div class="columns is-multiline">
<card v-for="card in uniqueCards" :card="card" :key="card.id"/>
</div>
</div>
</section>
</div>
</template>
<script>
import uniqBy from 'lodash/uniqBy'
import Card from "~/components/Card.vue";
export default {
components: {
Card
},
data() {
return {
appCards: []
};
},
async asyncData({ $axios, params }) {
try {
let appCards = await $axios.$get(`/appcards/`);
return { appCards }
} catch (e) {
return { appCards: [] };
}
},
computed: {
uniqueCards: function () {
return uniqBy(this.appCards, function (r) {
return r.project
})
}
}
};
</script>