2017-06-20 44 views

回答

0

我建议你对你的记录创建一个组件(参见https://vuejs.org/v2/guide/components.html

你可以用一个叫布尔每条记录创建一个组件,在组件的数据以及对V-开“紧接着” :单击指令编写一个三元条件,其中两个方法都会进行ajax调用,并且在成功回调时会改变布尔的状态。

Vue.component('follow-btn', { 
    template: '#follow-btn-template', 
    data: function() { 
    return { 
     followed: false 
    } 
    }, 
    methods: { 
    follow: function (dynamic_value) { 
     // Ajax call and on success => change this.followed to true 
    }, 
    unfollow: function (dynamic_value) { 
     // Ajax call and on success => change this.followed to false 
    } 
    } 
}) 

<script id='follow-btn-template' type="text/x-template"> 
    <button v-on:click="followed ? unfollow(dynamic_value) : follow(dynamic_value)"> 
    {{followed ? 'Following' : 'Follow'}} 
    </button> 
</script> 

或使用条件指令

<script id='follow-btn-template' type="text/x-template"> 
    <button v-if="followed" v-on:click="unfollow(dynamic_value)"> Following</button> 
    <button v-else v-on:click="follow(dynamic_value)"> Follow</button> 
</script> 

对于AJAX调用你要么使用jQuery,VUE资源或Axios公司。