【问题标题】:Uncaught TypeError: axios.ad is not a function in vue script未捕获的 TypeError:axios.ad 不是 vue 脚本中的函数
【发布时间】:2018-08-15 23:59:32
【问题描述】:

我的 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

【问题讨论】:

  • axios 没有名为 ad 的方法。你期待axios.ad(...) 做什么?
  • favorite() 和 unFavorite() 都接受 ad 道具作为参数。使用 Axios,我向路由(laravel 控制器路由)发出 POST 请求
  • 你不应该打电话给axios.post吗?如果您正在尝试发帖请求?
  • 没错!发布您的答案,以便我投票!谢谢
  • 我将投票结束这个问题,因为这似乎只是一个印刷错误,不太可能对未来的读者有所帮助

标签: laravel-5 vue.js


【解决方案1】:

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

【讨论】:

  • window.axios = require('axios'); window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
猜你喜欢
  • 2021-11-15
  • 2021-11-28
  • 2017-07-20
  • 2019-09-26
  • 2015-12-29
  • 2021-09-09
  • 2017-07-31
  • 2016-11-02
  • 2016-08-07
相关资源
最近更新 更多