2

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!

3 Answers 3

1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

But I still want my routes being managed by laravel, I only want to use vue-router to get the url parameters. Is that overkill? Should I just use jquery instead?
I think it is. Because this setup is for a single page application.
Yeah I just realised that. Thank you!
0

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;

Comments

0

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')

Comments

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.