简体   繁体   English

Vue.js:将项目作为参数传递给 v-for 中的计算道具,返回排序的子数组?

[英]Vue.js : passing item as parameter to computed prop in v-for, returning child array sorted ?

Have been googling around for quite a while to figure out how to do this..已经在谷歌上搜索了很长一段时间来弄清楚如何做到这一点..

I have a list of "Intents", which each of them got a list of "Entities", presented in a nested v-for.我有一个“意图”列表,其中每个都有一个“实体”列表,以嵌套的 v-for 呈现。

Intents are already computed, but i need to also sort the "Entities" list on the fly, so therefore i thought that making that list also computed..意图已经计算过了,但是我还需要动态地对“实体”列表进行排序,因此我认为制作该列表也可以计算..

Error :错误 :

**TypeError: _vm.entityList is not a function**

This is my current approach :这是我目前的做法:

 < script > import uniq from 'lodash/uniq' import orderby from 'lodash/orderby' import Util from "@/components/util.js" export default { data() { return { // .... } }, computed: { nluData() { return orderby(this.$store.getters.nlujson.filter(item => { return item.intent.toLowerCase() === this.selectedIntent }), ['intent', 'text'], ['asc', 'asc']) }, entityList(item) { return orderby(item.entities, ['entity', 'value'], ['asc', 'asc']) }, }, created() { this.$store.dispatch('getNluJson') }, methods: { // ...... } </script>
 // parent structure <div v-for="(item, key, index) in nluData"> // displaying nluData content through item.mydata // child structure <div v-for="ent in entityList(item)"> // displaying entities data through computed prop. // item.entities is the array </div> </div> { "id": "J4a9dGEBFtvEmO3Beq31", "text": "This is Intent 1", "intent": "shelf_life", "entities": [ { "start": "33", "end": "44", "value": "fridge", "entity": "ingredient_placement" }, { "start": "10", "end": "20", "value": "duration", "entity": "shelf_life" }, { "start": "25", "end": "30", "value": "spareribs", "entity": "ingredient" } ] },

You can pass parameters to a computed property with an anonymous function like so:您可以使用匿名函数将参数传递给计算属性,如下所示:

computed: {
  entityList() {
    return (item) =>
      orderby(item.entities, ['entity', 'value'], ['asc', 'asc']);
    },
  },

A computed property don't get any params.计算属性没有任何参数。 In your case, the prop entityList() must be a method :在您的情况下,道具entityList()必须是一种方法:

methods : {
    entityList(item) {
      return orderby(item.entities, ['entity', 'value'], ['asc', 'asc'])
    },
},

https://v2.vuejs.org/v2/guide/computed.html https://v2.vuejs.org/v2/guide/computed.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM