CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   Javascript   Laravel VueJS Tutorial – How to Set Up VueJS Inside La ...

Laravel VueJS Tutorial – How to Set Up VueJS Inside Laravel ?

March 17, 2018 by SNK

Laravel VueJS Tutorial Part 4

In this laravel vuejs tutorial we will see how to setup vuejs project inside laravel and prepare “ready to use” seed project which will use laravel as backend and vuejs as frontend.

I was searching for vuejs integration inside laravel rather than to use it seperately but unable to find one so, i made up this tutorial myself so that users like you can also learn from it.

VueJS comes integrated inside laravel and we just need to hit npm install in order to download all the dependencies and start using it.

But, we will prepare in such a way that it would give us a perfect knowledge and workflow on how the vuejs is working inside laravel.

Before proceeding through the tutorial make sure you have installed node package manger inside your system as well as fresh installation copy of a laravel app.

We are going to make clean copy of an laravel app integrated with vuejs so that we could use across many projects. We need to remove and clean up a lots of things from the fresh copy of laravel app to make a clean build.

Lets get started

Step 1 – Cleaning Up Un-necessary items

  1. Open package.json from your laravel root directory and remove bootstrap-sass, jquery and loadash lines from devDependencies section.
  2. Open app.scss from resources/assets/sass directory and cleanup & remove everything.
  3. Delete _variables.scss file fromĀ resources/assets/sass directory.
  4. Delete ExampleComponent.vue from resources/assets/js/components directory.
  5. Open app.js from resources/assets/js and remove all the codes.
  6. Open bootstrap.js from resources/assets/js and remove all the lines above window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';.

We will later create our own files and write our own codes during this tutorial which were removed in the above files.

Step 2 – Installing Vue-Router and devDependencies

We need routing in order to navigate through pages so we will need router in order to do so. vuejs doesnot have router by default, we need to install it by using npm.

Run cmd.exe and hit npm install vue-router --save-dev.

Your package.json file of the project will be updated with vue-router package in devDependencies section.

Now, hit npm install and wait while npm installs all the dependencies in your project. It will take a while to fetch all the dependencies inside the project.

Step 3 – Setting up Single Page and Routes

Now, we need to prepare a single page for our application.

Create new file called index.blade.php inside resources/views which will act as main entry point for our application. Paste these codes below.

index.blade.php
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="csrf-token" content="{{csrf_token()}}">
    <title>Codeinhouse Vue Laravel App</title>
    <link rel="stylesheet" href="{{url('css')}}/bootstrap.min.css">
    <link rel="stylesheet" href="{{url('css')}}/bootstrap-theme.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="{{url('css')}}/font-awesome.min.css">
    <link rel="stylesheet" href="/css/app.css">
 
</head>
<body>
 
<div id="app"></div>
 
 
<script src="{{url('js')}}/jquery-3.2.1.min.js"></script>
<script src="{{url('js')}}/bootstrap.min.js"></script>
<script src="/js/app.js"></script>
</body>
</html>

Time to setup routes. Open routes/web.php and paste these codes below :-

web.php
PHP
1
2
3
Route::get('/', function () {
    return view('index');
});

Now, open resources/assets/js/bootstrap.js and paste these code below just above the lineĀ window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

bootstrap.js
JavaScript
1
2
3
4
5
6
7
import Vue from 'vue';
import VueRouter from 'vue-router';
import axios from 'axios';
 
window.Vue = Vue;
Vue.use(VueRouter);
window.axios = axios;

Open resources/assets/js/app.js and paste these codes inside it.

app.js
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
require('./bootstrap');
 
import router from './routes';
import App from  './App.vue';
 
new Vue({
    el: '#app',
    router: router,
    template: '<App/>',
    components: {
        App
    }
});

Create new JavaScript file called routes.js inside resources/assets/js and paste these lines below. It will be used for routing for our single page application.

routes.js
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import VueRouter from 'vue-router';
import Home from './components/Home';
 
let routes = [
    {
        path:"/",
        component:Home
    },
];
 
 
export default new VueRouter({
    routes
});

Step 4 – Creating Components

We will create the base component as App.vue and inside it we will create sub components which will be easier to manage the components inside components.

Create new component inside resources/assets/js directory named App.vue and paste these lines inside it.

App.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<template>
    <div id="app">
        <cmp-navbar></cmp-navbar>
        <div class="container">
            <router-view />
        </div>
    </div>
</template>
 
<script>
    import Navbar from './components/partials/Navbar'
    export default {
        name: "app",
        components: {
            'cmp-navbar':Navbar
        }
    }
</script>
 
<style scoped>
 
</style>

Create new directory inside resources/assets/js/components named partials which will include the common things like header,footer,sidebar and navigation.

Lets create one partial component inside partials folder named Navbar.vue and paste these lines of code inside it.

Navbar.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<template>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <router-link to="/" class="navbar-brand">Database Directory</router-link>
            </div>
 
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <router-link to="/" activeClass="active" tag="li"><a>Home</a></router-link>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
</template>
 
<script>
    export default {
        name: "cmp-navbar"
    }
</script>
 
<style scoped>
 
</style>

Now, time to create a home component (main page component). Create new component named Home.vue inside resources/assets/js/components and paste these codes inside it.

Home.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
<template>
    <div align="center">Welcome to Vue Laravel</div>
</template>
 
<script>
    export default {
        name: "home"
    }
</script>
 
<style scoped>
 
</style>

 

Finally, hit npm run watch and php artisan serve from command line to boot-up your laravel vuejs app.

If you have any problem you can download seed project by clicking here.

Still stuck ? feel free to comment or contact us.

Next : Laravel VueJS 2 Crud Tutorial (Fetch Data from Database) – Part 1

SHARE ON
Buffer

Enjoyed this article?

Like us on

Javascript laravel 5.4 vue js crud laravel 5.4 vue js tutorial laravel 5.4 vue tutorial laravel 5.5 vue example laravel 5.5 vuejs laravel 5.5 vuejs tutorial laravel and vuejs tutorial laravel vue js crud example

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.