How to render a nested route from router link inside component? - laravel

this is a bit of an architecture question. I am fairly new to working with Vue and don't completely understand a good structure and architecture I need to use. I am using in within a Laravel application, if that helps.
I have a main file, index.php, where I have a Vue router instance. This has the following structure
<div>
<router-link to="/home"></router-link>
<router-link to="/about"></router-link>
<router-link to="/projects"></router-link>
<router-link to="/contact"></router-link>
</div>
<router-view></router-view>
This works fine and well. But, I want to extend the functionality. Each of these routes renders a corresponding component (ie HomeComponent, AboutComponent, etc). Within one component in particular (ProjectComponent) I want to have nested routes.
This is the nested route in question in my routes.js file:
{
path: '/projects',
component: ProjectComponent,
children: [
{
path: '/:project',
component: ProjectShowComponent,
}
]
},
So, when you click on the projects route and get to the ProjectComponent, you are presented with various projects that have their own router-link to that particular project. I thought because the ProjectComponent was an extension of the root Vue instance, that the router-link's within the file would still render the new component in the router-view, but that doesn't seem to work.
Am I totally off base with how I should architect something like this, or is there something in the routing that is off? Any help or thoughts would be appreciated. Thank you.

If you are using vue-router to do this then you'll have to have a look at passing props to the child component, here is the guide from vue router https://router.vuejs.org/guide/essentials/passing-props.html#boolean-mode
Another option would be to let laravel handle the routing of the application.

Related

How to structure vue.js components inside laravel blade view

I'm still learning vue.js and building a laravel/vue.js Ad placement application. I structured my app such that the landing page (a laravel blade view) has a large banner introducing the app and beneath a list of the ads.When a user click on an ad, it takes him/her to the single page of the Ad(this will be a component). My challenge is that I want the app to be a single page application but don't want the banner to show while displaying the single page component. I placed a root component in the welcome view and then inside the component i used router-view to import the components.
My issue is that when I click an Ad, the single page component loads with the banner. How do I make this layout so that the banner doesn't load with the single page component?
One solution would be to put the banner in a vue page followed by the list of ads, then have another vue page without the banner for the ad (pages handled by vue-router)
Another approach would be to have the ad links going to a route that's handled by laravel, then laravel serves a blade template without a banner and with an embedded vue app that just shows the relevant ad.
Vue also supports nested views so if you need several pages with the banner at the top a nested view below the banner in one type of page, and then another type of page with just the ad.
Hope that gives you some ideas to help solve the task.
Further to this: if you're building the ad placement functionality as a tightly coupled part of a whole website, then you might want to have the navbar for the site generated by Vue rather than having it in Blade, i.e. another Vue component for the navbar.
On the other hand, if you're trying to build a reusable Laravel package to do ad placement on any Laravel website, then you'd need to leave the navbar out of your Vue templates, and in your ad module use v-if/v-else rather than Vue-router because you don't want your code to fight the Laravel site for control of the routes.
e.g.
<template>
<div v-if="selectedAd">
//display selected ad
</div>
<div v-else>
//banner here
<div v-for="(ad, index) in ads" :key="index">
{{ ad.title }}
</div>
</div>
</template>
<script>
export default {
data() {
return {
ads: [],
selectedAd: null,
}
}
}
</script>
vue.js is spa framework not necessary do thing in blade all done inside vue.
search about vue router , vuex ,and do it easy good look.
You can use a v-on:click function of vue and set two variables for the ad and the other for the single page component.
Set the ad variable to true and the page component variable to false and when a use clicks on the ad the function switches the ad variable to false amd the page component variable to true.

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.

Including Vue.js into 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.

VueJs view in laravel

I wonder how can i get my page parts such as sidebar or header with Vue way?
currently I get them like #include('admin.parts.header') which is blade template, but is it possible i move them to components (header.vue) and get them with vue way like <router-view name="header"></router-view> ?
The correct way to import via #include. You need to import vue components only with vue, blade with blade.
the solution was to have div with id around my sidebar.vue (eg) template and call that id instead of in my main component.

Vue.js SPA with Laravel API

I have a problem with defining routes for frontend portion of the application. It is Vue.js with Vue Router. Since it is SPA I think defining all routes in Laravel is not the correct answer but it seems I need to somehow because content is not served with current setup. Does Vue Router fetch the entire page and load it with each link? That would mean setting up a route for each link to load in Laravel routes. I think this is not intention of SPA decide.
So with route like
<route-link :to="{name: 'faq'}">FAQ</route-link>
Does this mean I must make a route
Route::get('faq', 'FaqController#index')->name('faq');
This is new architecture that is not well understand with me now. Thank you.
Generally, SPA setup will fallback to "index.html" when no matched route
In routes/web.php:
Route::get('{path}', function () {
return view('index');
})->where('path', '(.*)');
and vue-router will handle the rest
full setup: https://github.com/cretueusebiu/laravel-vue-spa/blob/master/routes/web.php

Resources