Laravel 5.4 和 Vue 2.0.1 无法读取未定义的 属性 'get'

Laravel 5.4 and Vue 2.0.1 Cannot read property 'get' of undefined

我正在使用带有新 Laravel 5.4 的 Vue 模板。在我的 Office.vue 我有这个代码。

<template>
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">Example Component</div>

                    <div class="panel-body">
                        I'm an example component! {{ message}} | {{ type}} | {{ number }}
                    </div>
                    <span id="vue"></span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        mounted() {
            return this.displayOffices();
        },
        data: function(){
            return { 
                message: "aw",
                type: "test",
                number: 2 
            }
        },
        methods: {
            displayOffices: function(){
                this.$http.get('/vueOffice', function(course) {
                    console.log(course);
                });
            }
        }
    }
</script>

显示数据很好,工作也很好,但

出现错误

Uncaught TypeError: Cannot read property 'get' of undefined

this.$http.get

我想要完成的是从 returns JSON 数据的路线获取数据。

我需要帮助修复此错误,一直在尝试寻找答案,但每个人在实现 Vuejs 方面都有不同的方法。

您必须安装 vue-resource 才能使用 this.$http.get

或者您可以安装 axios 并使用 axios.get 而不是 this.$http.get

不推荐使用 this.$http.get,因为 Vue 想要放弃对它的支持。他们建议使用使用 axios 的库。然后您可以使用 this.axios.get()

self.axios
  .get(apiUrl, {
    params: this.formData,
  })
  .then((response) => {

  });

如果您使用 axios 而不是 vue-resource,则将 this.$http 替换为 axios

另一种方法是将以下代码添加到您的文件中:

Vue.prototype.$http = axios;

在你的文件中添加这个 require('vue-resource');


/resources/assets/js/bootstrap.js

之后:

window.Vue = require('vue');