Including Vue.js into Laravel - laravel

I have seen some tutorial on how to include Vue.js into Laravel projects.
As far as I can see the tutorials are using Vue.js by creating templates and importing them into the blade-files. Is there anything "wrong" in just including the Vue javascript file directly into blade-files and using Vue directly like that? In other words just using:
<script src="https://cdn.jsdelivr.net/npm/vue#2.5.17/dist/vue.js"></script>
in the blade-files I want to use Vue.
The reason for this is that I just want Vue.js to replace some of the work that has been done by Jquery in the application. I have also been thinking about using React. But it seems like that goes the same way as Vue (creating templates, which then should be imported into the corresponding blade-files).
I use Laravel 5.6.
Thanks for any guidance!

Great, I will try to explain my approach of doing such kind of replacement work.
Laravel comes with assets directory containing js and css directory. If you explore more the js directory you'd see two files bootstrap.js and app.js.
bootstrap.js file is nothing but few handy stuffs like setting jQuery as global object so that it can be accessed in vue instance easily, setting axios default header to have X-CSRF-TOKEN while using axios in the project.
But the important part is app.js. It initialises the vue instance and binds it with a #app div which mostly the first div after <body> tag.
require('./bootstrap');
window.Vue = require('vue');
/**
* Next, we will create a fresh Vue application instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
Vue.component('example', require('./components/Example.vue'));
const app = new Vue({
el: '#app'
});
In the above file, you can see there vue instance is created and it is bound to a div whose id is app. Along with that Vue is also registering a component called example which is stored under js/components/ directory as Example.vue.
Now, based on your requirement you can go ahead and follow these steps to create components to replace some of jQuery codes without having any conflicts.
• Make sure to bind the the vue instance to root div so that you can use the component in the blade file this way.
<div id="something">
<example></example>
</div>
• If you Laravel mix, make sure you compile the app.js
mix.js('resources/assets/js/app.js', 'public/js')
I have worked on projects have similar required modifications, I followed the way I just mentioned and worked quite well for me.
Feel free to ask any question you have. Cheers!

Im not entirely sure I fully understand what you are trying to achieve, but you will have to new Vue in every blade file then since you have a classic page reload when you switch from one blade file to the next (if they're not included in each other). I imagine that this is going to give you a lot of trouble sharing data between your various vue instances.

Related

vue does not display .svg file in local env

I am making some project with Laravel, Vue2 now.
I deployed my project onto the test server, there .svg files display, but in my local environment they don't display.
I use :src="asset('images/ico_arrow_left1.svg')" in vue component.
My images and svg files are all in root/public/images folder.
For example; I wrote the following code to display svg in vue component.
<a #click="previousDay"><img class="ico_ico_arrow" :src="asset('images/ico_arrow_left1.svg')"></a>
The codes are sampe in test server and local.
show in test server, but not show in local, why?
If you want import a file from public folder you should check the documentation. You should use require when you import dynamic asset. Like this:
<img class="ico_ico_arrow" :src="require('images/ico_arrow_left1.svg')"/>
The asset() helper function is only for the Laravel Blade template. you can't use it inside of a regular Vue application.
In Vue, you should use require()in order to solve this problem.

Include Vue in a Codeigniter project

I have a very broad project, developed in codeigniter, I want to integrate Vue and I do not know how to do it.
Is it better to create a new project based on Vue and try to migrate things?
Modify my JS of my current project and replace it with Vue?
What do you recommend?
i have sucessfully use vue to replace codeigniter view layer, i use laravel mixin to compile vue SFC then include built file to the codeigniter view template
FrontEnd frameworks has nothing to do backend frameworks. They communicate via http-request responses.
You may use backend of your choice and render the content on the page.
However, if you want to integrate vue with existing project (which is already working), I recommend new project based on Vue and migrate back end code on need basis

How can I access the value from a Vue component?

I am trying to make my portfolio using Laravel. I am very new to Laravel. I thought by making a mini project in it will make me familiar with Laravel. Recently, I figured out that Laravel has a dependency on Vue.js
I tried to include jQuery libraries as I am not at all familiar with Vue. But that didn't go so well. Then I started searching for Vue.js but I couldn't find much.
With a lot of effort I made the following Vue component:
var trialEle = new Vue({
el: '#trialEle',
data: {
working: 'yes'
}
});
And then I tried to access the value of working by the following markup:
<div id="trialEle">
<h1>Is this working: {{working}}</h1>
</div>
But I don't know what I am missing because I am constantly having this error:
UPDATE:
As suggested by #Chamara in the answer below, I have changed {{working}} to #{{working}}, now there is no error screen.
But still I cannot access the value of working from my Vue component.
It is displayed on the page as follows:
Is this working: {{working}}
Any help will be truly appreciated.
Thanks in advance.
{{}} is a built in syntax in blade if you need to use vue {{}} inside your blade file you need to put # in front of the {{}}. So like this#{{working}}. # in blade ignore it as a syntax.

Using Laravel includes inside a vue component?

I have a set of components, some components include other components via #include('my-sub-component')
The issue is that when I want to use vue I have to put the entire component in the vue.js file inside the template tags.
But inside the template I want to use the #include so I can include some sub components. This does not work.
How can I include blade components inside a vue component?
As far as I know, it's impossible. You can't use PHP (Laravel Blade) .blade.php tags inside Vue Component .js, of course Vue can't execute PHP script in this case. The possible use case is you use the Vue Component inside Laravel Blade.
The closest thing you can get to this is to use inline templates. You can't access blade directives it if you are writing your templates in single file components and compiling them with webpack or something because they are not being rendered with PHP.
You could put the style and scripts components in the SFC though and but in order to use blade directives the template would need to be in a .blade.php file

how to uses ReactJS and Laravel

how to combine Laravel and React? I mean, Laravel uses blade for its view, how to change it to React instead of blade ? There have been so many Laravel and Angular tutorial, but I can't find any for React and Laravel?
Any references of Laravel and ReactJS tutorial ?
If you are using Laravel to build a REST web service (API), then you might not need Blade. You just return JSON data. Your React App can then make calls to the Laravel API for data and process it right in the browser. You don't need Blade for this.
You would use Laravel to provide the base view (index.blade.php) which would include the React JS app, but then after that all the views and routing would be handled by React.
Here's a link to get you started.
https://medium.com/#rajikaimal/laravel-loves-reactjs-8c02f8b9d1d5
I have created laravel reactjs starter project .. refer the github project below.
I am not using the mix api of laravel since I want reactjs app to be portable to any other server or standalone app
refer the link below
https://medium.com/#padmanabhavn/bringing-laravel-and-reactjs-together-8c970cb0f65d
This question seems old but I will still share my experience for others.
This tutorial and project sample may be found useful.
https://blog.pusher.com/react-laravel-application/
https://github.com/maarcz/taskman
As well as that, the solution below has worked for me in webpack.mix.js without using app.js to be mixed up with other requirements (laravel 8):
mix.js('resources/assets/js/react', 'public/js/reactJs').react()
in blade:
<script src="{{ asset('js/reactJs/react.js') }}"></script>

Resources