How can I use Laravel's baseurl on the vue.js 2? How can I use Laravel's baseurl on the vue.js 2? vue.js vue.js

How can I use Laravel's baseurl on the vue.js 2?


You will need to attach that to window. I think the confusion comes because Laravel makes the csrf_token available via Laravel.csrfToken but to do that it places the following in the header:

    window.Laravel = <?php echo json_encode([            'csrfToken' => csrf_token(),    ]); ?>

Personally, I don't like mixing php with javascript, but if you really want to add the baseurl to your global Laravel object, you could just do:

    window.Laravel = <?php echo json_encode([            'csrfToken' => csrf_token(),            'baseUrl' => base_url()    ]); ?>

Here's the JSFiddle: https://jsfiddle.net/w6szt02q/


Set env variables in your webpack config.

module.exports = {  NODE_ENV: '"production"',  BASE_URL: '"http://myshop.ct-staging.tk'",}// config/dev.env.jsmodule.exports = merge(prodEnv, {  NODE_ENV: '"development"',  BASE_URL: '"http://myshop.dev/"' // this overrides the BASE_URL value of prod.env})

Use it in your Vue app.

Vue.config.baseurl = process.env.BASE_URL