Is there a way to define a dynamic route with fixed set of values? And if it doesn't fit any of the fixed values it would fallback to the next route. My current is like this -
const routes = {
path: '',
component: AppComponent,
childRoutes: [
{ path: '/search/top', name: 'top', component: FixedSearchComponent},
{ path: '/search/new', name: 'new', component: FixedSearchComponent},
{ path: '/search', name: 'search', component: SearchComponent},
{ path: '/search/:query', name: 'search', component: SearchComponent},
]
}
But I'd like to define a parameter for it like :fixedSearch maybe and have it predefined to only be this values. Maybe something like this? And if it doesn't fit any of top or new (or other possible set), it would fallback to search.
const routes = {
path: '',
component: AppComponent,
childRoutes: [
{ path: '/search/:fixedSearch', name: 'fixedSearch', fixedSearch: ['top', 'new'], component: FixedSearchComponent},
{ path: '/search', name: 'search', component: SearchComponent},
{ path: '/search/:query', name: 'search', component: SearchComponent},
]
}
You could attach an onEnter function a new route, like /fixed/:fixedSearch that replaces /search/new and /search/top. Inside the onEnter function you would compare :fixedSearch to your predefined values (['top', 'new']) and if they don't match you can fallback to your /search route. This is common for checking if a user is authenticated before allowing access to a route.
Here's the documentation for onEnter:
onEnter(nextState, replace, callback?)
Called when a route is about to be entered. It provides the next
router state and a function to redirect to another path. this will be
the route instance that triggered the hook.
If callback is listed as a 3rd argument, this hook will run
asynchronously, and the transition will block until callback is
called.
A rough example (in JSX) might look like:
<Route path='/fixed/:fixedSearch' component={FixedSearchComponent} onEnter={checkFixedSearch} />
function checkFixedSearch(nextState, replace) {
if (*Compare :fixedSearch with predefined values*) {
replace('/search') // move to search route if fixed values don't match
}
}
Related
I'm trying to figure out the cleanest way to set up my Angular routing.
I have a parent route, and inside as a child, I'd like to repeat the same component multiple times (a list of all the locations). Each of the location (child) component occurrences will look the same, just with different data that I will need to pass in. (I'll do this with *ngFor).
Here's what I'm thinking so far:
<router-outlet>//for the parent
<router-outlet name="aux">
<ul *ngFor="let location in service.locations"
</router-outlet>
//where I list out all the locations^
</router-outlet>
on my app-routing.module.ts file:
const routes: Routes = [
{ path: 'main', component: MainComponent }, //lists all locations
{ path: 'locations/:id', component: LocationsComponent }, //view 1
location
{ path: '', pathMatch: 'full', redirectTo: '/main},
];
(I want to have both parent and child appear on initial load. Should I modify that?^)
Now this is the part I don't understand as much, I need to set my route configs. Is this close? And does it goin my app-routing.module.ts file as well?
#RouteConfig([
{path:'/', name: 'MainPath', component: MainComponent,
useAsDefault: true},
{aux:'/auxRoute', name: 'AuxPath', component: SecondComponent}
])
Thanks! Please let me know if you need clarification.
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
}
]
}
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
I have a list of path I want to apply CanActivate to all path except some path.Is there any way in angular to discard some path.
Currently I am applying canActivate to all the URLs.If there are lots of URL,We don't want to apply it to all the URL, I will apply to parent path.
But if we apply to parent it will be applicable to all children also. Is there any way to discard some of the children.
const routes: Routes = [
{ path : '', component: UsersListComponent, canActivate:[AuthenticationGuard] },
{ path : 'add', component : AddComponent, canActivate:[AuthenticationGuard]},
{ path : ':id', component: UserShowComponent },
{ path : 'delete/:id', component : DeleteComponent, canActivate:[AuthenticationGuard] },
{ path : 'ban/:id', component : BanComponent, canActivate:[AuthenticationGuard] },
{ path : 'edit/:id', component : EditComponent, canActivate:[AuthenticationGuard] }
];
It is not possible to discard some children, but you may break your routes in such a way that Guard applies to some and not to some,
Try below,
{
path: 'parentPath',
component: ParentComponent,
canActivate: [AuthGuard],
children: [
{path : 'child1', component: ChildComponent1},
{path : 'child2', component: ChildComponent2}
]
},
{
path: 'parentPath',
component: ParentComponent,
children: [
{path : 'child3', component: ChildComponent3}
]
}
If you design your routes like above, Guards will only be applied to child1 and child2, and not on child3.
In this way you can easily apply Guard at parent level for some of the children.
Here is a Plunker!!
Suppose there is a component - UsersComponent, and it has two methods: getAlUsers() and getUser(id). In the #RouteConfig we can use only one name of the component (constructor will be called default ), but we can not specify what kind of a class method must be called. Is it possible to define something like this:
{
path: '/users/',
component: UsersComponent,
name: 'Users'
},
{
path: '/users/getAllUsers',
component: UsersComponent.getAllUsers,
name: 'GetAllUsers'
},
{
path: '/users/getUser',
component: UsersComponent.getUser,
name: 'GetUsers'
}
Or is it impossible to make means Angular 2?
This is a pretty common use case for component nesting and using sub components for the different actions like "user list" and "user details". The idea is to use pretty fine grained components with a very focused use case. If you have common code (e.g. loading data via http), consider moving this logic into a service that is used by all components.
You can achieve this by referencing a "UserComponent" in your top level Component routing with the "..." notation. Then inside this "UserComponent" define another routing with two configs for / and /:id such that you reference two child components "user list" and "user details".
Parent Route Config:
{
path: '/users/...',
name: 'Users',
component: UserComponent
}
Route Config in the UserComponent:
#RouteConfig([
{ path: "/", name: "User List", component: UserListComponent, useAsDefault: true },
{ path: "/:id", name: "User Detail", component: UserDetailComponent },
])