[英]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.我鼓励您检查map
和sort
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.