Hillcow
Hillcow

Reputation: 979

Laravel Vue.js - easiest way to include vue-router?

In bootstrap.js, I have this:

window.Vue = require('vue');
window.events = new Vue();

Now I would like to use vue-router as well purely to have access to this.$route. What is the easiest way to do this? I tried to follow the official documentation, but it's so much different than what comes with laravel:

const app = new Vue({
router
}).$mount('#app')

Thank you!

Upvotes: 2

Views: 842

Answers (3)

East2d
East2d

Reputation: 2096

Here are steps to make laravel routes and vue routes work together:

1. define your vue layout

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Project</title>
    <link rel="stylesheet" href="/css/app.css">
</head>

<body>
    <div id="app"></div>
    <script src="/js/app.js"></script>
</body>
</html>

2. define your laravel route to make vue-router handle url

Route::any('{all}', function(){
    // welcome view should extend layout defined in step #1
    return view('welcome');
})->where('all', '.*');

3. setup vue with vue-router

routes.js

import DashboardPage from './components/dashboard/dashboard.vue'
import SignupPage from './components/auth/signup.vue'
import SigninPage from './components/auth/signin.vue'

export const routes = [
  { path: '/signup', component: SignupPage },
  { path: '/signin', component: SigninPage },
  { path: '/dashboard', component: DashboardPage },
  { path: '*', redirec: '/' }
];

router.js

import VueRouter from 'vue-router';
import { routes } from './routes';

export const router = new VueRouter({
  routes,
  mode: 'history'
});

export default router

main.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import { router } from './router'

Vue.use(VueRouter);

new Vue({
  router: router,
  render: h => h(App)
}).$mount('#app')

Upvotes: 0

Ikbel
Ikbel

Reputation: 7851

First install vue-router.

Then create a new file router.js in resources/assets/js and put this code in it.

import Router from 'vue-router'

Vue.use(Router)

/*
   Make sure your pages components are inside `resources/assets/js/pages` folder
*/
const Home = require('./pages/Home.vue')
const Hello = require('./pages/Hello.vue')
const NotFound = require('./pages/NotFound.vue')

let router = new Router({
    mode: 'history',
    routes: [
    {
        path: '/',
        name: 'home',
        component: Home
    }, {
        path: '/hello',
        name: 'hello',
        component: Hello
    }, {
        path: '*',
        component: NotFound
    }, ]
})

export default router

Now go to app.js file and insert this code:

import Vue from 'vue'
import router from './router.js'

const app = new Vue({
    el: '#app',
    router, // Add this to your Vue instance
    //...
})

Then create your pages (Home.vue, Hello.vue and NotFound.vue in this case).

Upvotes: 1

yixiang
yixiang

Reputation: 930

in app.js

import VueRouter from 'vue-router';
window.Vue = require('vue');
window.Vue.use(VueRouter);
import router from './config/routes';
const app = new Vue({
    el: '#app',
    router,
});

you could extract the code for routes definition into ./config/routes.js, see below example:

import VueRouter from 'vue-router';
const routes = [
    {path: '/', redirect: '/app/articles'},
    ...
];

const router = new VueRouter({routes});
export default router;

Upvotes: 0

Related Questions