Skip to main content

Laravel Vue Basic (Use of Vue Router)

 Vue Router Documentation: https://router.vuejs.org/installation.html

1) In terminal run the command: 

npm install vue-router

2) As we will have many routes, so we will make a new file for that inside resources -> js create

router.js file.

3) Open router.js file and import Vue & Router:

import Vue from 'vue'
import Router from 'vue-router'


Now, we will use the route. So, in router.js file:

Vue.use(Router)

const routes = [
    {
        path: '/my-new-vue-route',
        component: 
    }
];

Here, we have given a path url name (you can give anything as url name). and the component is blank.

we will first make a folder called pages inside resources -> js -> components

and, create a file inside pages folder called myFirstVuePage.vue .

4) Inside myFirstVuePage.vue file write:

<template>
  <div>
    <h1>This is our first page</h1>
  </div>
</template>


5) Now, again go back to router.js file, we will import this component.

Write import in router.js file like the following example and use in component:

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import firstPage from './components/pages/myFirstVuePage'

const routes = [
    {
        path: '/my-new-vue-route',
        component: firstPage
    }
];

export default new Router({
    mode: 'history',
    routes
});


6) and we need to import routes in app.js file: 

require('./bootstrap');

window.Vue = require('vue');
import router from './router'
Vue.component('mainapp'require('./components/mainapp.vue').default);

const app = new Vue({
    el: '#app',
    router
});

 

7) Inside mainapp.vue write:

<template>
  <div>
    <h1>This is the first component</h1>
    <router-view></router-view>
  </div>
</template>

8) In terminal run: npm run watch

9) Now hit the /my-new-vue-route and you will see the the component is loaded successfully.



Comments

Popular posts from this blog

SQL: sneak peek

show databases; use mysql; show tables; select * from component; describe component; create database sql_intro; show databases; use sql_intro; create table emp_details (Name varchar(25), Age int, gender char(1), doj date, city varchar(15), salary float); describe emp_details; insert into emp_details  values("Jimmy",35,"M","2005-05-30","Chicago",70000), ("Shane",30,"M","1999-06-25","Seattle",55000), ("Marry",28,"F","2009-03-10","Boston",62000), ("Dwayne",37,"M", "2011-07-12","Austin", 57000), ("Sara",32,"F","2017-10-27","New York",72000), ("Ammy",35,"F","2014-12-20","Seattle",80000); select * from emp_details; select distinct city from emp_details; select count(name) as count_name from emp_details; select avg(salary) from emp_details; select name, age...