最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js pass bound data to click event handler - Stack Overflow

programmeradmin3浏览0评论

I have a v-for loop where I loop through some data returned from my api and set the key equal to the id of the object in the api. Now I want to create a click event that gets the v-bind:key value of the clicked element. This is so that I can use it to find all the data for this object in my posts[] array. But I don't know if it is possible to do this, if so how?

Here's my code;

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

        <div id="summary_board">
            <div class="column">
                <div class="head">
                    <h3>Opportunities</h3>
                </div>

                <div class="body">
                    <div class="post" 
                        v-for="post in posts"
                        v-bind:key="post._id"
                        v-on:click="getPostData"
                    >
                        <p>{{ post._id }}</p>
                        <p class="pany">{{ postpany_name }}</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import axios from 'axios';

    export default {
        data() {
            return{
                posts: []
            };
        },
        created() {
            axios.get('http://localhost:5000/')
            .then(res => {
                console.log(res.data);
                const data = res.data;
                this.posts = data;
            })
            .catch(error => console.log(error));
        },
        methods: {
            UpdatePosts() {
                axios.get('http://localhost:5000/')
                    .then(res => {
                        // console.log(res);
                        const data = res.data;
                        this.posts = data;
                    })
                    .catch(error => console.log(error));
            },
            getPostData(event) {
                console.log(event);
            }
        }
    }
</script>

I have a v-for loop where I loop through some data returned from my api and set the key equal to the id of the object in the api. Now I want to create a click event that gets the v-bind:key value of the clicked element. This is so that I can use it to find all the data for this object in my posts[] array. But I don't know if it is possible to do this, if so how?

Here's my code;

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

        <div id="summary_board">
            <div class="column">
                <div class="head">
                    <h3>Opportunities</h3>
                </div>

                <div class="body">
                    <div class="post" 
                        v-for="post in posts"
                        v-bind:key="post._id"
                        v-on:click="getPostData"
                    >
                        <p>{{ post._id }}</p>
                        <p class="pany">{{ post.pany_name }}</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import axios from 'axios';

    export default {
        data() {
            return{
                posts: []
            };
        },
        created() {
            axios.get('http://localhost:5000/')
            .then(res => {
                console.log(res.data);
                const data = res.data;
                this.posts = data;
            })
            .catch(error => console.log(error));
        },
        methods: {
            UpdatePosts() {
                axios.get('http://localhost:5000/')
                    .then(res => {
                        // console.log(res);
                        const data = res.data;
                        this.posts = data;
                    })
                    .catch(error => console.log(error));
            },
            getPostData(event) {
                console.log(event);
            }
        }
    }
</script>
Share Improve this question edited Mar 24, 2020 at 13:28 Boussadjra Brahim 1 asked Mar 24, 2020 at 13:08 ReeceReece 2,72111 gold badges53 silver badges105 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Just pass that id as parameter inside the event click handler as follows :

 v-on:click="getPostData($event,post._id)"

the method handler :

 getPostData(event,id) {
            console.log(event,id);
}

if you don't need the event object you could do that like :

 v-on:click="getPostData(post._id)"   

and

 getPostData(id) {
            console.log(id);
}

You can update your template like:

v-on:click="getPostData(post._id)"

and then access this value like:

getPostData(id) {
    console.log(id);
}

Also, @click is just shorthand for v-on:click directive, so you can also use that if you like:

@click="getPostData(post._id)"
发布评论

评论列表(0)

  1. 暂无评论