'Vue.js resource not working with Laravel 5.3 out-of-box config

Working with fresh install of Laravel 5.3 with Vue.js included and configured out-of-the-box:

this.$http.get(url) returns error:

TypeError: this$1.$http is undefined

Replacing that with Vue.http.get(url)...makes the request but fails to send preflight options:

missing token ‘x-csrf-token’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel

app.js:

require('./bootstrap');
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('content');

let app = new Vue({
        el: '#app',

        mounted: () => {
            navigator.geolocation.getCurrentPosition((position) => {
                app.getZip(position);
            });
        },

        methods: {
            getZip: (position) => {
                let lat = position.coords.latitude;
                let long = position.coords.longitude;
                let url = encodeURI(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${long}&sensor=false`);
                Vue.http.get(url).then((response) => {
                    let zip = response.body.results[0].address_components[5].short_name;
                    console.log(zip);
                });
            },
        },
    });

bootstrap.js:

window._ = require('lodash');

window.$ = window.jQuery = require('jquery');
require('bootstrap-sass');

window.Vue = require('vue');
require('vue-resource');

Vue.http.interceptors.push((request, next) => {
    request.headers.set('X-CSRF-TOKEN', Laravel.csrfToken);

    next();
});

Relevant app.blade.php:

<meta id="token" name="csrf-token" content="{{ csrf_token() }}">
<script>
        window.Laravel = <?php echo json_encode([
            'csrfToken' => csrf_token(),
        ]); ?>
</script>

<script src="/js/app.js"></script>

gulpfile.js:

const elixir = require('laravel-elixir');

require('laravel-elixir-vue-2');

elixir(mix => {
    mix.sass('app.scss')
       .webpack('app.js');
});

If I include <script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/1.0.3/vue-resource.min.js"></script> it will successfully make the request with both:

Vue.http.get(url)
this.$http.get(url)

Why isn't this working with the node_module? require('vue-resource')



Solution 1:[1]

Its because you are using require. If you use require you need some library like browserify or webpack, which allows us to have node.js-style modules that compile for use in the browser.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel