2

I'm trying to get vue-router to work without success and it got me quite angry after a while since i don't see any issue.

I'm using webpack via Elixir in Laravel, which has gulpfile like this:

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

require('laravel-elixir-vue');

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

Into my app.js file I'm "compiling" following code

window.Vue = require('vue');

var VueResource = require('vue-resource');
var VueRouter = require('vue-router');

Vue.use(VueResource);
Vue.use(VueRouter);

And when I call in browser console

new VueRouter({})

I got an error saying VueRouter is undefined. Why? The vue-resource is even in the app.js.

My package.json looks like this:

{
  "private": true,
  "scripts": {
    "prod": "gulp --production",
    "dev": "gulp watch"
  },
  "devDependencies": {
    "bootstrap-sass": "^3.3.7",
    "foundation-sites": "^6.2.3",
    "gulp": "^3.9.1",
    "jquery": "^2.2.4",
    "laravel-elixir": "^6.0.0-9",
    "laravel-elixir-vue": "^0.1.4",
    "laravel-elixir-webpack-official": "^1.0.2",
    "lodash": "^4.14.0",
    "script-loader": "^0.7.0",
    "vue": "^2.0.1",
    "vue-resource": "^0.9.3",
    "vue-router": "^2.0.0"
  }
}

Can anyone help?

1 Answer 1

1

thats okay cause you didnt bind VueRouter to the window object as you did with window.Vue = require('vue');

But usually you dont need the Router in the window Do it in your app.js:

window.Vue = require('vue');

var VueRouter = require('vue-router');
window.Vue.use(VueRouter);

var router = new VueRouter({
  routes: {
    {path: '', component: require('./components/MyComponent.vue')},
    {path: '/dash', component: require('./components/Dash.vue')},
  }
});

var AppComponent = require('./components/App.vue');

/* eslint-disable no-new */
var app = new Vue({
  el: '#app',
  router,
  render: h => h(AppComponent),
});
Sign up to request clarification or add additional context in comments.

2 Comments

So what's the solution? You're saying I should bind it this way window.Vue.use(VueRouter)? Because if Vue is not binded to window, it doesn't work at all...
Can you spare few minutes on skype or hangout? It still doesn't go for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.