[英]Computed property on Nuxtjs and Axios?
I want to return an array of unique cards by name. 我想按名称返回一组唯一的卡片。 I was able to do this in Vuejs using the following code.
我可以使用以下代码在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>
I am converting my project to Nuxtjs and using an api to get appCards. 我将我的项目转换为Nuxtjs,并使用api获取appCards。 How do I achieve the same functionality as above using axios?
如何使用axios实现与上述相同的功能? Below piece works, but I want to use the uniqueCards() function.
下面的作品,但我想使用uniqueCards()函数。 What am I doing wrong here?
我在这里做错了什么?
<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>
Nevermind, got it to work! 没关系,让它起作用! I forgot the proper syntax.
我忘记了正确的语法。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.