Vue Router : Cant define children for default OR main components - javascript

First of all I'm learning Vue JS 2 since about a week and I'm stuck on a problem with Vue Router. I'm pretty sure the solution is right in front of my eyes but I wasn't able to find it yet.
So : On my root route I have 2 views in components Home and NotAuth.
My aim is to display the Login component and others routes related to it when the user isn't logged.
And when the user logs in the Home component is displayed on '/' and all login related routes are not accessible by him.
It works when I'm logged if I try to access '/register' Im instantly redirected to 'Home'.
But when I'm not logged if I try to access say '/add_device' a blank page is displayed.
What I'd like to do is define other routes as Home's children but I cant find anything in the doc explaining how to do so.
I've tried several things and none worked and I'm kind of lost on how to proceed.
If anyone has a clue on how to proceed that would be awesome.
Thanks !!
index.js
const routes = [
{
path: '/',
name: '',
components: {
login: NotAuth,
default: Home
},
children: [
{
path: '/',
name: 'Login',
component: Login
},
{
path: '/register',
name: 'Register',
component: Register
},
{
path: '/reset',
name: 'ResetPassword',
component: ResetPassword
},
{
path: '/reset-sent',
name: 'ResetSent',
component: ResetSent
},
{
path: '/change-password/:token/:mail',
name: 'ChangePassword',
component: ChangePassword
},
{
path: '/confirmation',
name: 'Confirmation',
component: Confirmation
}
]
},
{
path: '/add_device',
name: 'device',
component: Device
},
{
path: '/add_scene',
name: 'scene',
component: Scene
},
{
path: '/add_room',
name: 'room',
component: Room
},
]
App.vue
<div id="app">
<router-view v-if='userAuthenticated' :username='this.user.first_name' :user='this.user'></router-view>
<router-view name="login" v-else #isAuth='handle'></router-view>
</div>

Related

True or False to show route-item in navbar

im building a website and using vvue with vue-router.
My Navbar component iterates through the routes element and gets all routes to show in the navbar, so i can simply add and remove routes without changing the navbar component.
Now i want for example the Data protection notice or the legal notice not to show up.
I tried to solve this with a boolean and a v-if. Do you have any idea how to solve this problem?
My code looks as following:
Navbar component:
<template>
<div>
<div class="mdheader"></div>
<div class="md-layout-item">
<md-tabs md-sync-route class="md-primary" md-alignment="centered">
<div
v-for="r in this.$router.options.routes"
:key="r.name"
:v-if="r.showOnBar"
>
<md-tab :md-label="r.name" :to="r.path" exact></md-tab>
</div>
</md-tabs>
</div>
</div>
</template>
<script>
export default {
name: "navbar"
};
</script>
router/index.js
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "#/views/Home.vue";
Vue.use(VueRouter);
const routes = [
{
path: "/",
name: "Home",
component: Home,
showOnBar: true
},
{
path: "/about",
name: "About",
showOnBar: true,
component: () => import("#/views/About.vue")
},
{
path: "/aktuelles",
name: "Aktuelles",
showOnBar: true,
component: () => import("#/views/Aktuelles.vue")
},
{
path: "/datenschutz",
name: "Datenschutz",
showOnBar: false,
component: () => import("#/views/Datenschutz.vue")
}
];
const router = new VueRouter({
routes
});
export default router;
Thanks in advance
Youa re very close, but you need to wrap your custom property in meta option:
const routes = [
{
path: "/",
name: "Home",
component: Home,
meta: { showOnBar: true }
},
{
path: "/about",
name: "About",
meta: { showOnBar: true }
component: () => import("#/views/About.vue")
},
{
path: "/aktuelles",
name: "Aktuelles",
meta: { showOnBar: true },
component: () => import("#/views/Aktuelles.vue")
},
{
path: "/datenschutz",
name: "Datenschutz",
meta: { showOnBar: false }
component: () => import("#/views/Datenschutz.vue")
}
];
and access it via route.meta.showOnBar. Keep in mind that you only need to define it for routes you want to show on your navbar.
You need to wrap your custom route properties into meta as stated in BroiSatse's answer.
However, another issue is that you used :v-if (i.e. you bound the v-if attribute) instead of just using v-if. v-if already evaluates the condition in the value as per the docs.
Here's a sandbox to play around with: https://codesandbox.io/s/stack-overflow-q-62409392-6zovw?file=/src/router/index.js

Ionic 4 and using material tabs router outlet

I am wanting to use Material Tab's (https://material.angular.io/components/tabs/api#MatTabLink) within my Ionic 4 project, now, the requirements are that I need to house multiple views in a tab and the first thought was that I can use a new ion-router-outlet or router-outlet within my parent component.
Bare in mind that I do already have one router outlet for the main app.
I am lazy loading the main chat routes in my app-routing.module.ts, this page is responsible for loading the tabs.
{ path: 'chat', loadChildren: './chat/chat.module#ChatPageModule', canActivate: [ AuthGuard ]}
Now, in my chat.module.ts I have the following routes:
{ path: '', component: ChatPage },
{ path: 'active', component: ActivePage },
{ path: 'messages', component: MessagesPage },
{ path: 'teams', component: TeamsPage }
ChatPage component is my parent tab view page. The others I am wanting to be in a tab.
The HTML for displaying these tabs is in chat.page.html and looks like this:
<nav mat-tab-nav-bar>
<a mat-tab-link
*ngFor="let link of routeLinks"
[routerLink]="link.path"
routerLinkActive #rla="routerLinkActive"
[active]="rla.isActive">
{{ link.label }}
</a>
</nav>
<router-outlet></router-outlet>
I have also tried <ion-router-outlet></ion-router-outlet> but this throws up more issues.
The main issue here is that the routes look as though they are loading up in the main router outlet rather than the child one, I have tried adding the name attribute to the mark up but my IDE states that it's not valid and doesn't seem to work.
Ok, I have figured it out, and I am going to look stupid for not trying this before but the issue was that in order to use this child router-outlet the routes I wanted in tabs need to child routes.
{ path: '', component: ChatPage, children: [
{ path: 'active', component: ActivePage },
{ path: 'messages', component: MessagesPage },
{ path: 'teams', component: TeamsPage }
] },

Angular 6 Aux Routing

So the idea is that I'm navigating to an admin page, & then want to load various components within that page using a side nav only on that page.
So I'm pretty sure an aux route is the best case for this, tell me if I'm wrong.
{ path: 'userAccounts', component: UserManagementComponent, canActivate: [LoggedInGuard]},
{ path: 'admin', component: AccountCreationPageComponent, outlet:'admin' },
<div class="navigation_items" *ngFor="let x of data"
[routerLink]="[ { outlets: { 'admin':['admin']}}]">{{x.id}}</div>
<router-outlet name="admin"></router-outlet>
Basically I'm assuming my syntax is wrong somewhere, or I haven't quite understood how Aux routes work, as this wont display the admin path
Any help would be appreciated.
It would appear that the child route had to be nested within the parent route so
{
path: 'userAccounts', component: UserManagementComponent, canActivate: [LoggedInGuard], children: [
{ path: 'admin', component: AccountCreationPageComponent, outlet: 'admin' }
]
}
Did the trick.

Use child routes, but do not render them in a router-outlet

I have a code where router config looks like:
{
path: 'users',
component: UsersComponent,
children: [
{
path: 'add',
component: AddUserComponent
}
]
}
By that I'd like to have two pages:
/users - which shows users list
/users/add - which shows a form to add a new user
But I don't what to have nested router-outlet tags. Both pages I'd like to render in the same, main container which is app-root. I know I can do that just by:
{
path: 'users',
component: UsersComponent
},
{
path: 'users/add',
component: AddUserComponent
}
But you probably agree with me that this is not a nice solution and it'd be good to use children construction.
How can I do that?
You don't need a nested route if you don't have a component assigned to the route => just don't add the component to the definition of the users route and make a dedicated empty path for the list page:
{
path: 'users',
children: [
{
path: '',
component: UsersComponent
},
{
path: 'add',
component: AddUserComponent
}
]
}
There is a drawback here: if you use a routerLink inside of UsersComponent where you will use relative path, the empty path will behave still as a path. So you would need to use relative path similar to ../add and not ./add as normally one would think
Check this code:
{
path: 'users',
component: UsersComponent,
children: [
{
path: '',
component: UsersComponent
},
{
path: 'add',
component: AddUserComponent
}
]
}

Angular - URL changed but view not loaded

This is my app-routing-module.ts
const routes: Routes = [
{
path: '',
component: LayoutComponent,
children: [
{
path: 'parent',
component: ParentComponent,
children: [
{ path: ':id/:name', component: ChildComponent }
]
}
]
},
];
I've written a function to check if the URL is working with a static value in parent.component.ts.
goToChild() {
this.router.navigate(['1/john'], { relativeTo: this.route });
}
And I call the function in the parent.component.html.
<button class="btn btn-primary" (click)="goToChild()">Search</button>
When I click the button, I get the correct URL in the address bar,
localhost:3000/parent/1/john
But the view never loads, it stays on the parent.component.html. I'm fairly new to Angular and I'm using version 5.
And if I have my routes like this, it works fine.
const routes: Routes = [
{
path: '',
component: LayoutComponent,
children: [
{
path: 'parent',
component: ParentComponent
},
{
path: 'parent/:id/:name', component: ChildComponent
}
]
},
];
It feels more appropriate for me to put the ChildComponent route under children array of the ParentComponent, but when I do it. Only the URL changes, not the view.
Any help will be much appreciated.
You need to add a <router-outlet></router-outlet> inside the template. That’s how you allow child navigation

Categories