未捕获的TypeError:axios.ad不是vue脚本中的函数

时间:2018-03-07 19:03:44

标签: laravel-5 vue.js

我的vue脚本出现此错误,这是我正在使用的文件 找不到axios.ad(favoriteAd.vue)报告的错误。 它基本上是一个受欢迎的功能: https://scotch.io/tutorials/implement-a-favoriting-feature-using-laravel-and-vue-js#comments-section (用广告替换帖子)

app.js

require('./bootstrap');

window.Vue = require('vue');

Vue.component('favorite', require('./components/FavoriteAd.vue'));

const app = new Vue({
  el: '#favorite'
});

favoriteAd.vue

 <template>
<span>
    <a href="#" v-if="isFavorited" @click.prevent="unFavorite(ad)">
        <i  class="fa fa-heart"></i>
    </a>
    <a href="#" v-else @click.prevent="favorite(ad)">
        <i  class="fa fa-heart-o"></i>
    </a>
</span>
</template>

<script>
export default {
    props: ['ad', 'favorited'],

    data: function() {
        return {
            isFavorited: '',
        }
    },

    mounted() {
        this.isFavorited = this.isFavorite ? true : false;
    },

    computed: {
        isFavorite() {
            return this.favorited;
        },
    },

    methods: {
        favorite(ad) {
            console.log(ad);
            axios.ad('/favorite/'+ad) // this is the error line
                .then(response => this.isFavorited = true)
                .catch(response => console.log(response.data));
        },

        unFavorite(ad) {
            axios.ad('/unfavorite/'+ad)
                .then(response => this.isFavorited = false)
                .catch(response => console.log(response.data));
        }
    }
}

查看

@if (Auth::check())
 <favorite
    :ad={{ $aviso->id }}
    :favorited={{ $aviso->favorited() ? 'true' : 'false' }}
></favorite>   
@endif

1 个答案:

答案 0 :(得分:-2)

您从未在应用中添加过axios。在app.js文件上尝试npm install axios然后require('./axios')。此外,根据axios参考手册,没有名为ad的方法。