简体   繁体   English

如何在 JavaScript 过滤器函数中访问 Vue.js 数据变量

[英]How to access Vue.js data variables inside a JavaScript filter function

I have a v-for function that loops through my posts and returns some posts that have a specific value using a filter function.我有一个 v-for 函数,它循环遍历我的帖子并使用过滤器函数返回一些具有特定值的帖子。 Inside this function I also want to capture some of the post data and push it into a new array inside my data() function.在这个函数中,我还想捕获一些发布数据并将其推送到我的data()函数中的一个新数组中。 But I'm getting an error saying;但我收到一条错误消息;

Cannot read property 'opportunityHeadValues' of undefined"无法读取未定义的属性“opportunityHeadValues””

Here's my code:这是我的代码:

<template>
    <div id="summary_section">
        <h2>Summary</h2>

        <div id="summary_board">
            <div class="column">
                <div class="head">
                    <p class="column_title">Opportunities</p>
                    <p class="column_percentage">0%</p>
                    <hr>
                    <p class="calculated_total">£0</p>
                    <p class="raw_total">£000000</p>
                </div>

                <div class="body">
                    <div class="post"
                        v-for="post in sortPosts('Opportunity')"
                        v-bind:key="post._id"
                        v-on:click="toggleUpdateFormVisibility(post)"
                    >
                        <p>{{ post._id }}</p>
                        <p class="company">{{ post.company_name }}</p>
                    </div>
                </div>
            </div>

            <div class="column">
                <div class="head">
                    <h3>Prospects</h3>
                </div>

                <div class="body">
                    <div class="post" 
                        v-for="post in sortPosts('Prospects')"
                        v-bind:key="post._id"
                        v-on:click="toggleUpdateFormVisibility(post)"
                    >
                        <p>{{ post._id }}</p>
                        <p class="company">{{ post.company_name }}</p>
                    </div>
                </div>
            </div>

            <div class="column">
                <div class="head">
                    <h3>Proposal</h3>
                </div>

                <div class="body">
                    <div class="post" 
                        v-for="post in sortPosts('Proposal')"
                        v-bind:key="post._id"
                        v-on:click="toggleUpdateFormVisibility(post)"
                    >
                        <p>{{ post._id }}</p>
                        <p class="company">{{ post.company_name }}</p>
                    </div>
                </div>
            </div>

            <div class="column">
                <div class="head">
                    <h3>Presentation</h3>
                </div>

                <div class="body">
                    <div class="post" 
                        v-for="post in sortPosts('Presentation')"
                        v-bind:key="post._id"
                        v-on:click="toggleUpdateFormVisibility(post)"
                    >
                        <p>{{ post._id }}</p>
                        <p class="company">{{ post.company_name }}</p>
                    </div>
                </div>
            </div>
        </div>

        <SubmitForm v-if="submitFormVisibility" v-on:formSubmitted="newFormSubmission" v-on:closeModal="toggleSubmitFormVisibility"/>
        <UpdateForm 
            v-if="updateFormVisibility" 
            v-on:formSubmitted="updateFormSubmission" 
            v-on:closeModal="toggleUpdateFormVisibility" 
            v-on:opportunityDeleted="updateFormSubmission" 
            v-bind:postData="post"
        />
    </div>
</template>

<script>
    import SubmitForm from './SubmitForm.vue';
    import UpdateForm from './UpdateForm.vue';
    import axios from 'axios';

    export default {
        components: {
            SubmitForm,
            UpdateForm
        },
        data() {
            return{
                posts: [],
                post: [],
                submitFormVisibility: false,
                updateFormVisibility: false,
                opportunityHeadValues: []
            };
        },
        created() {
            this.getPostData();
        },
        methods: {
            getPostData() {
                axios.get('http://localhost:5000/')
                    .then(res => {
                        const data = res.data;
                        this.posts = data;
                    })
                    .catch(error => console.log(error));
            },
            toggleSubmitFormVisibility(){
                this.submitFormVisibility = !this.submitFormVisibility;
            },
            toggleUpdateFormVisibility(post){
                if( post != undefined ) {
                    this.post = post;
                }
                this.updateFormVisibility = !this.updateFormVisibility;
            },
            newFormSubmission() {
                this.getPostData();
                this.toggleSubmitFormVisibility();
            },
            updateFormSubmission() {
                this.getPostData();
                this.toggleUpdateFormVisibility();
            },
            sortPosts(columnName) {
                return this.posts.filter( function(post) {
                    if( columnName == 'Opportunity') {
                        this.opportunityHeadValues.push({annual_value: post.annual_value});
                    }

                    if(post.pipeline_stage == columnName) {
                        return post;
                    }
                });
            }
        }
    }
</script>

Simply replace function with arrow functions, so sortPosts will be written in the following syntax:简单地用箭头函数替换函数,所以sortPosts将用以下语法编写:

            sortPosts(columnName) {
                return this.posts.filter((post) => {
                    if( columnName == 'Opportunity') {
                        this.opportunityHeadValues.push({annual_value: post.annual_value});
                    }

                    if(post.pipeline_stage == columnName) {
                        return post;
                    }
                });
            }

And another point, filter expects boolean to be returned, so it keep all items that return true and filter out all items that return false .还有一点, filter期望返回boolean ,因此它保留所有返回true项目并过滤掉所有返回false项目。 I encourage you to check map and sort functions.我鼓励您检查mapsort功能。

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

相关问题 如何将变量传递给 vue.js 模板中的函数? - How to pass variables to function inside of vue.js template? Javascript - 如何在 Vue.js 中使用多个 arrays 过滤数据? - Javascript - How to filter data with multiple arrays in Vue.js? 如何访问存储在此 Javascript 数组 (vue.js) 中的变量? - How can I get access to the variables stored in this Javascript Array (vue.js)? Laravel Blade 和 Vue.js,在模板中连接 php 函数和 vue.js javascript 变量 - Laravel blade and Vue.js, concatenate php function and vue.js javascript variable inside a template 如何在 vue.js 中访问 setIntervall 函数之外的变量? - How do I access variables outside a setIntervall function in vue.js? 是否可以在 Function (forEach) 中打印一些东西? Vue.Js,JavaScript - Is it possible to print something inside a Function (forEach)? Vue.Js, JavaScript Vue.js-如何将参数传递给计算属性内的JavaScript过滤器函数? - Vue.js - How to pass parameters to a JavaScript filter function within a computed property? 如何从Vue.js中的组件生命周期方法访问mixin方法中的函数 - How to access function inside methods of mixin from component lifecycle method in Vue.js Javascript - 如何在 Vue.js 中使用多个复选框进行过滤? - Javascript - How to filter with multiple checkboxes in Vue.js? Vue.js - 从回调函数访问数据变量 - Vue.js - Access data variable from callback function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM