In this article we will see how to setup vue with laravel.
1) Create a laravel project:
laravel new laravue
2) In web.php create a temporary route:
So, when you will see the welcome page once you hit the http://laravue.test/test url.
2) In terminal run:
npm install
3) npm install vue
(In package.json file you will see that vue dependency has been added successfully)
4) Now go to resources -> js -> app.js file. Here we will have to load the vue.
write the following lines in app.js.
(Here, vue js will execute and control anything that will be inside the app id. So, for our example
let's put the app id inside the welcome view).
So, inside welcome.blade.php 's body part write:
5) Now we will create a component. Component is one of the powerful feature of vue js. It is a way to
create dynamic , reusable code etc.
inside resources -> js folder create a folder called components and inside components folder
create mainapp.vue file.
Inside mainapp.vue file write:
To use the mainapp.vue component go to app.js and write:
Here, 'mainapp' is the name that we will use to call the component. The name can be anything. and we declared the path of the component in require method and set it as default.
6) Now we will set the mainapp component inside app div of welcome.blade.php view:
8) Now, If you hit the /test url , Though all of the things are done it won't work as because
vue doesn't know what is mainapp until we compile it with webpack.mix.js file.
Open webpack.mix.js file. There you will see:
Which means, take everything from app.js file of resources -> js folder and put them inside the app.js file of public -> js folder.
Run in terminal: npm run watch
(It will compile the webpack.mix.js file)
9) Now, in welcome.blade.php file include the script:
(Here, the js/app.js is present inside the public folder).
Now, Hit the /test url of project in the browser. You will see the text of mainapp.vue component.


Comments
Post a Comment