Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueRouter is not defined

I have a laravel project and want use Vue.js for frontend. But i have never used something more complicated than jquery. I can't run vue-router.

In my app.js

require('./bootstrap');
require('./vue-router');
require('./routes');

window.Vue = require('vue');
     const app = new Vue({
     el: '#app',
});

Then i copy code vue-router from https://router.vuejs.org/en/installation.html

In my routes.js

const router = new VueRouter();

var App = Vue.extend({});

router.start(App, '#app');

And when i try open page in console i see next : "VueRouter is not defined"

like image 299
Viktor Avatar asked Dec 04 '17 13:12

Viktor


People also ask

Does Vue router 4 work with Vue 2?

It's not possible to use vue-router@4 with vue@2 . They're incompatible with each other.


1 Answers

You have to tell Vue to use VueRouter using the method Vue.use() first. So, do:

import VueRouter from 'vue-router'

# add this code
Vue.use(VueRouter)

var router = new VueRouter({
  routes: [
    {path: 'home', component: homeComponent} 
  ]
})

UPDATED:

First install vue-router using

npm install --save vue-router

Then import and use it like

import VueRouter from 'vue-router'

Then use it in vue

Vue.use(VueRouter)

Then define your routes:

const routes = [
 {path: '/', component: SomeComponent}
]

then initialize the router and pass it the routes

var router = new VueRouter({
  routes: routes,
  mode: 'history'
})

Pass router to vuejs then profit :)

new Vue({
  el: '#root',
  router: router
})
like image 81
samayo Avatar answered Sep 22 '22 21:09

samayo