简体   繁体   English

Vuejs2 - 如何将两个数组中的元素与计算属性进行比较?

[英]Vuejs2 - How to compare elements in two array with computed properties?

I'm working on a computed function for comparing two arrays... one keywords in data and another returned by computed keywords_list .我正在研究一个用于比较两个数组的计算函数……一个keywords在数据中,另一个由计算的keywords_list返回。 When computed compare, he doesn't take in count values getting before by Axios in the first array ... I don't know what to do ...当计算比较时,他不接受 Axios 在第一个数组中获得的计数值......我不知道该怎么做......

my code:我的代码:

{
    beforeCreate() {
            this.$axios({
                method: 'get',
                url: '/api/rest/alerts/'
            }).then(response => {
                if ((response.data.keywords.length == 0) && (response.data.countries.length == 0)) {
                    this.alertEmpty = 'Aucune alerte de créée'
                } else {
                    this.alert = response.data
                    this.keywords = response.data.keywords
                    this.countriesKeywords = response.data.countries
                    this.allSectorsSelected = response.data.all_sectors
                }
            })
        },

        data() {
            return {
                categories: "",
                alert: '',
                alertEmpty: '',
                countriesKeywords: [],
                countrySelected: '',
                keywords: [],
                update: false,
                sectorSelected: "",
                alerts: [],
                keywordSelected: "",
                errorKeyword: '',
                successModification: '',
                allSectorsSelected: null,

            };
        },

        computed: {
            keywords_list: function() {
                for (var i = 0; i < this.sectors.length; i++) {
                    if (this.sectors[i].id == this.sectorSelected) {
                        return this.sectors[i].keywords;
                    }
                }
            },
            allKeywordsInSector: function() {
                if (this.keywords_list.every(item => this.keywords.indexOf(item) >= 0)) {
                    return true;
                }
                return false;
            },
        }
}

thanks for your help感谢您的帮助

very little information and it is unclear how the api result affects your computed values The only thing i can say, properties in Vue doesn't reactive.信息非常少,并且不清楚 api 结果如何影响您的计算值 我只能说,Vue 中的属性没有反应性。 So if sections isn't static, computed property sectorSelected will change only if sectorSelected changes所以如果sections不是静态的,计算属性sectorSelected只有在sectorSelected改变时才会改变

https://v2.vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow https://v2.vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow

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

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