Component doesn't load while the route changes - vuejs2

I want to create a nested route with its children's route.
Basically what I want is,
https://localhost/contact render the ContactList component.
https://localhost/contact/add render the ContactAdd component.
What I have tried is:
let Layout = {
template: '<div>Layout Page <router-view></router-view></div>'
};
let Home = {
template: '<div>this is the home page. Go to <router-link to="/contact">contact</router-link> </div>'
};
let ContactList = {
template: '<div>this is contact list, click <router-link to="/contact/add">here</router-link> here to add contact</div>'
};
let ContactAdd = {
template: '<div>Contact Add</div>'
}
let router = new VueRouter({
routes: [{
path: '/',
redirect: 'home',
component: Layout,
children: [{
path: 'home',
component: Home
},
{
path: 'contact',
component: ContactList,
children: [{
path: 'add',
component: ContactAdd
}]
},
]
}]
});
new Vue({
el: '#app',
components: {
'App': {
template: '<div><router-view></router-view></div>'
},
},
router
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-router#3.0.1/dist/vue-router.js"></script>
<section id="app">
<app></app>
</section>
Here, the problem is when I change the route from /client to /client/add, the view doesn't render. What is the problem with the nested children's route here? How to solve this issue?
I check this documentation https://router.vuejs.org/guide/essentials/nested-routes.html, but It didn't help in this case.

You need to add one <router-view> in the template of <ContactList> to load its children route.
Or if you'd like to display ContactAdd in Layout, moves ContactAdd to direct child of path=/.
let Layout = {
template: '<div>Layout Page <router-view></router-view></div>'
};
let Home = {
template: '<div>this is the home page. Go to <router-link to="/contact">contact</router-link> </div>'
};
let ContactList = {
// add <router-view> in order to load children route of path='/contact'
template: '<div>this is contact list, click <router-link to="/contact/add">Add Contact In sub Router-View</router-link> here to add contact<p><router-view></router-view></p> Or Click <router-link to="/addcontact">Add Contact In Current Router-View</router-link></div>'
};
let ContactAdd = {
template: '<div>Contact Add</div>'
}
let router = new VueRouter({
routes: [{
path: '/',
redirect: 'home',
component: Layout,
children: [{
path: 'home',
component: Home
},
{
path: 'contact',
component: ContactList,
children: [{
path: 'add',
component: ContactAdd
}]
},
{
path: 'addcontact', // or move ContactAdd as direct child route of path=`/`
component: ContactAdd,
}
]
}]
});
new Vue({
el: '#app',
components: {
'App': {
template: '<div><router-view></router-view></div>'
},
},
router
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-router#3.0.1/dist/vue-router.js"></script>
<section id="app">
<app></app>
</section>

Related

Hide sidebar component some page in VUE.js

I am working with Vue.js and I have a question.
Is there a way to hide sidebar component only some page ?
This is my code below.
router.js
const routes = [
{
path: '/',
redirect: '/home',
component: () => import('#/layout/MainLayout'),
children: [
{
path: 'home',
name: 'Home',
component: () => import('Views/Home.vue'),
},
],
},
{
path: '/subpage',
redirect: '/subpage/subpage',
component: () => import('#/layout/MainLayout'),
children: [
{
path: 'subpage',
name: 'Subpage',
component: () => import('Views/Subpage/Subpage.vue'),
},
],
},
];
MainLayout.vue
<template>
<div id="content" class="content">
<router-view></router-view>
<Sidebar></Sidebar>
</div>
</template>
<script>
import Sidebar from '#/components/Sidebar.vue';
export default {
components: {
Sidebar,
},
};
</script>
Do I need to make another layout? like MainLayout2.vue
Please help.
You can add meta information to vue routes. Add the sidebar key to the routes you want to hide
{
path: '/subpage',
redirect: '/subpage/subpage',
component: () => import('#/layout/MainLayout'),
children: [
{
path: 'subpage',
name: 'Subpage',
meta:{sidebar:false}
component: () => import('Views/Subpage/Subpage.vue'),
},
],
},
Then in your layout component, make a computed property to check if the current route needs to hide the sidebar
computed:{
shouldShowSidebar(){
return this.$route.meta.sidebar!==false;
}
}
Then use this computed property to conditionally hide the sidebar on specific routes
<Sidebar v-if="shouldShowSidebar"></Sidebar>
You can add meta:{sidebar:false} to any route you want the sidebar to be hidden on

how to open new link in master page in vuejs

I have navigation drawer and this nav contained number of pages ..i want to add new page that will be contain another url but open in the same page with master page...is that possible ???
fev.vue file
<template>
<v-app>
<div class="feb">
<v-card>
<div> {{ callfeb}} </div>
</v-card>
</div>
</v-app>
</template>
export default {
computed: {
callfeb(){
href = 'https://######',
target='_self'
//window.location.href = 'https://#####'
}
}
}`</script>`<style></style>``
App.vue
computed: {
items() {
return [
{
icon: 'link',
text: i18n.t('H-D'),
path: '/feb',
perms: 'read:feb',
show: true
},
]
}
router.ts
router [
{
path: '/feb',
name: 'feb',
props: route => ({
query: route.query,
isKiosk: route.query.kiosk,
hash: route.hash
}),
component: () =>
import(/* webpackChunkName: 'user' */ './views/feb.vue'),
meta: { title: 'feb', requiresAuth: true }
},
]
the above codes are working and open the url in same page but the master content will not show. I want the url will open in the same page with master content

When navigating to a router via an alias, how to change the URL to the path?

When the user navigates to a page using an alias of the route, the URL stays with the alias and the page is rendered correctly.
How could I change to URL to the path when the user navigates to an alias?
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "#/views/Home.vue";
Vue.use(VueRouter);
const routes = [
{
path: "/404",
name: "404",
alias: "*",
component: () => import(/* webpackChunkName: "NotFound" */ "#/views/NotFound.vue")
},
{
path: "/",
name: "Home",
alias: "/home",
component: Home
},
{
path: "/downloads",
name: "Downloads",
alias: ['/download', '/releases'],
component: () => import(/* webpackChunkName: "Downloads" */ "#/views/Downloads.vue")
}
];
const router = new VueRouter({
mode: "history",
base: process.env.BASE_URL,
routes
});
export default router;
For example:
User types www.example.com/home.
Browser loads the Home page page.
URL gets changed to 'www.example.com/'.
Other:
User types www.example.com/lalallaa.
Browser loads the NotFound page page.
URL gets changed to 'www.example.com/404'.
What would be the best way to accomplish this? I can only think about using the created() event on each page and manually setting the URL, but I'm not sure if it's the best way.
What you want are redirects, not aliases.
Set up the known redirects you want like
/home => /
/download and /releases => /downloads
and then add a catch-all redirect to /404 at the end of the routes array.
const router = new VueRouter({
routes: [{
path: '/404',
component: { template: `<h1>404</h1>` }
}, {
path: '/',
component: { template: `<h1>Home</h1>` }
}, {
path: '/home', redirect: '/'
}, {
path: '/downloads',
component: { template: `<h1>Downloads</h1>` }
}, {
path: '/download', redirect: '/downloads'
}, {
path: '/releases', redirect: '/downloads'
}, {
path: '*', redirect: '/404'
}]
})
new Vue({
el: '#app',
router
})
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
<script src="https://unpkg.com/vue-router#3.1.6/dist/vue-router.js"></script>
<div id="app">
<ul>
<li><router-link to="/home">/home</router-link></li>
<li><router-link to="/">/</router-link></li>
<li><router-link to="/downloads">/downloads</router-link></li>
<li><router-link to="/download">/download</router-link></li>
<li><router-link to="/releases">/releases</router-link></li>
<li><router-link to="/lalallaa">/lalallaa</router-link></li>
</ul>
<router-view></router-view>
<pre>$route.fullPath = {{ $route.fullPath }}</pre>
</div>

Vue Router: Redirects don't work with history mode

For some reason, a regular redirect rule does't works with history mode, but works well with hash mode.
Here is a code snippet:
let first = {
name: "first",
template: `<section><h1>First</h1></section>`
};
let second = {
name: "second",
template: `<section><h2>Second</h2></section>`
};
new Vue({
el: "#app",
router: new VueRouter(
{
mode: "history",
routes: [
{ path: "/", redirect: { name: 'first' }}, // Works with hash mode, but does't with history
{ path: "/first", component: first, name: "first"},
{ path: "/second", component: second, name: "second" }
]
}
)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.10/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.1.3/vue-router.min.js"></script>
<div id="app">
<router-link to="/first">First</router-link>
<router-link to="/second">Second</router-link>
<router-view></router-view>
</div>
Why?

Vuejs - How to pass params from child route to parent route?

I am a beginner in vuejs.
I have an issue with some practice about vue-router nested route.
Just in case, This is a nested route config.
const router = new Router({
routes: [
{
path: '/dashboard',
name: 'dashboard',
component: Dashboard,
meta: {
requiresAuth: true
},
children: [
{
path: 'overview',
name: 'Overview',
component: Overview,
meta: {
requiresAuth: true
}
}
]
}
]
})
If I want to pass data from a child route (overview) to parent route (dashboard)
How should I handle it ?
I don't want the url have any change (/dashboard/overview), just receive the data params from child route.
You might want to consider using Vuex as a means of sharing state across different components (or maybe not if you're a beginner).
Alternatively you can just emit an event in the child component:
Vue.use(VueRouter);
var Parent = {
template: '<router-view #data="onData"></router-view>',
methods: {
onData(data) {
alert('Got data from child: ' + data);
},
},
};
var Child = {
template: '<div><input v-model="text"><button #click="onClick">Click Me</button></div>',
data() {
return {
text: 'Hello',
};
},
methods: {
onClick() {
this.$emit('data', this.text);
},
},
};
var router = new VueRouter({
routes: [
{
path: '',
component: Parent,
children: [
{
path: '',
component: Child,
},
],
},
],
});
new Vue({
el: '#app',
router,
});
<script src="https://rawgit.com/vuejs/vue/dev/dist/vue.js"></script>
<script src="https://rawgit.com/vuejs/vue-router/dev/dist/vue-router.js"></script>
<div id="app">
<router-view></router-view>
</div>