Angular 2 [routerLink] navigates but doesn't update the url - html

My Link
<a [routerLink]="['/login']">
<i class="glyphicon glyphicon-user"></i> Login
</a>
My Routes
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: UserRegisterComponent },
{ path: 'auctions', component: AuctionListComponent },
{ path: 'auction/:id', canActivate: [ AuctionDetailGuard ], component: AuctionDetailComponent },
{ path: 'supplier/:id', component: SupplierDetailComponent }
];
the routerlinks that navigates to auctions, auction/:id and supplier/:id works as intended, but login and register does not, when i click either one of them i get navigated to the correct component, the URL however changes to localhost:xxxx/login for a split second before it goes back to localhost:xxxx/previousUrl
Here is the other routerlink, that works as intended
<a [routerLink]="['/auction', auction.id]">
{{category.name }} - {{auction.name}}
</a>

When you have child routes, it's better to use the children: [].
This is how you use it:
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: UserRegisterComponent },
{ path: 'auctions', component: AuctionListComponent, children: [
{ path: ':id', canActivate: [ AuctionDetailGuard ], component: AuctionDetailComponent },
{ path: ':id/edit', component: AuctionDetailEditComponent }
]},
{ path: 'supplier/:id', component: SupplierDetailComponent }
];
// This is what you can place on the item
<a [routerLink]="[idOfItem]">Link to Item</a>
// And this method you call in your component.ts file
// Add relativeTo to make it relative to the current route
// so you'll add the "edit" after the called ID.
this.router.navigate(['edit'], { relativeTo: this.route });

Can you please try login and register in the following way:
<a routerLink="/login">
<i class="glyphicon glyphicon-user"></i> Login
</a>

Related

Angular RouteLinkActive for child components not working

i have Component IndexSettings with childs Component, I need when they go to the child component so that the asset class works, but it disappears, can you know what is the reason? thanks for the help
Index Settings Component Routes
children: [
{
path: 'settings',
component: SettingsIndexComponent,
children: [
{ path: '', redirectTo: 'company-details', pathMatch: 'full' },
{
path: 'company-details',
component: CompanyDetailsComponent,
},
{
path: 'team',
component: TeamComponent,
},
{
path: 'password',
component: PasswordComponent,
},
{
path: 'plans-pricing',
component: PlansPricingComponent,
},
{
path: 'payments-billing',
component: PaymentsBillingComponent,
},
{
path: 'documents',
component: DocumentsComponent,
},
{
path: 'notifications',
component: NotificationsComponent,
},
],
},
Html routeLinkActive
<a
[routerLink]="['/dashboard/settings']" [routerLinkActive]="['sidebar-active']" [routerLinkActiveOptions]="{exact: true}"
class="list-group-item border-0 d-inline-block ps-4 custom-nav-item mt-2">
<i class="fa-light fa-sliders-simple custom-nav-item"></i>
<span class="ps-2 custom-nav-item">Settings</span>
</a>

angular routerlink redirect to 404

I have a problem with routerlinks in angular. I use routerlinks to redirect to the next page in my application.
If I start my applicaton with ng serve everything works fine. If I build my application with ng build --prod routerlinks return in some cases a 404.
The exact problem:
I am using a routeroutlet to display the different components. If I click on a routerlink which is in a component that is displayed in the routeroutlet I get a 404. If I use a routerlink that is in my component.html outside of the routeroutlet everything works fine.
That is my routeroutlet
<div class="">
<router-outlet></router-outlet>
</div>
That is my routerlink
<a [routerLink]="['overview']" [queryParams]="{categorie:'Shopping',subcategorie:'topcategorie'}"
class="btn btn-primary searchbutton">suchen</a>
and that are my routes:
const routes: Routes = [
{ path: 'overview/:categorie/:subcategorie', component: OverviewComponent },
{ path: 'overview', component: OverviewComponent },
{ path: 'overview/:brand', component: OverviewComponent },
{ path: 'overview/:product', component: OverviewComponent },
{ path: 'coupons', component: CouponComponent },
{ path: 'impressum', component: ImpressumComponent },
{ path: 'datenschutz', component: DatenschutzComponent },
{ path: 'details/:id', component: ProductDetailsComponent },
{ path: 'home', component: LandingpageComponent },
{ path: '', component: LandingpageComponent }
];
#NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

Angular 6 - issue with nested routes

I'm trying to setup up nested routes for an admin parent menu. Below admin I have two child routes - Security Settings and Configuration.
When I click on the Configuration menu item, my page is redirected to AdminComponent and renders Admin works. However, if I click on Site Configuration, it does NOT redirect to my SiteConfigComponent as I expect it to; it just redirects to AdminComponent again.
i.e. Here's the siteconfig route snippet which is also shown again below.
<a mat-button routerLink="/admin/siteconfig">
Site Configuration
</a>
HTML:
<a mat-button routerLink="/home" routerLinkActive="horizontal-active-link" [routerLinkActiveOptions]="{exact:true}">Home</a>
<a mat-button routerLink="/preferences" routerLinkActive="horizontal-active-link" [routerLinkActiveOptions]="{exact:true}">Preferences</a>
<a mat-button [matMenuTriggerFor]="appMenu">Admin
<mat-icon class="caret">arrow_drop_down</mat-icon>
</a>
<mat-menu #appMenu="matMenu" [overlapTrigger]="false" class="mega-menu app-dropdown">
<div fxLayout="column wrap">
<div fxFlex class="p-1">
<a mat-menu-item routerLink="/admin" routerLinkActive="horizontal-active-link" [routerLinkActiveOptions]="{exact:true}">
<b>Configuration</b>
</a>
<mat-divider></mat-divider>
<button mat-menu-item>Security Settings</button>
<a mat-button routerLink="/admin/siteconfig" routerLinkActive="horizontal-active-link">
Site Configuration
</a>
</div>
</div>
</mat-menu>
export const routes: Routes = [
{
path: '',
component: PagesComponent, children: [
{ path: '', component:HomeComponent, canActivate: [AuthGuard]},
{ path: 'home', component: HomeComponent, canActivate: [AuthGuard]},
{ path: 'admin', component: AdminComponent,
children: [
{ path: '', component: AdminComponent },
{ path: 'management/users', component: UsersManagementComponent, canActivate: [AuthGuard] },
{ path: 'management/roles', component: RolesManagementComponent, canActivate: [AuthGuard] },
{ path: 'siteconfig', component: SiteConfigComponent, canActivate: [AuthGuard]}
],
canLoad: [AuthGuard]
}
]
},
{ path: 'login', component: LoginComponent},
{ path: '**', component: NotFoundComponent }
];
And app.component.html (loaded by index.html) is :
<div id="app" class="100h app" [ngClass]="appSession.settings.Theme">
<router-outlet></router-outlet>
</div>
Again, if I click on Site Config it should go to the SiteConfigComonent.
Advice appreciated...

How can i add a new component and its routing in ngx-admin-starter-kit?

I want to create a whole new web application using ngx-admin theme.
I have created a new component named "OPERATION". But, I'm not able to get the routing for it.
Need help in routing..
Thanks in advance...
first you need to import your component and you need to add your component route to the routes in the app-routing.module.ts.
below is the routes in ngx admin app-routing.module.ts.
const routes: Routes = [
{
path: 'pages',
loadChildren: () => import('./pages/pages.module')
.then(m => m.PagesModule),
},
{
path: 'auth',
component: NbAuthComponent,
children: [
{
path: '',
component: NbLoginComponent,
},
{
path: 'login',
component: NbLoginComponent,
},
{
path: 'register',
component: NbRegisterComponent,
},
{
path: 'logout',
component: NbLogoutComponent,
},
{
path: 'request-password',
component: NbRequestPasswordComponent,
},
{
path: 'reset-password',
component: NbResetPasswordComponent,
},
],
},
{ path: '', redirectTo: 'pages', pathMatch: 'full' },
{ path: '**', redirectTo: 'pages' },
];

Replace semicolon, id and equal sign in URL in an Angular2 app

I am trying to set URLs to my items got via json.
So I have a structure like that:
<div class="releases-component">
<div *ngFor="let release of releases" [routerLink]="['/releases', { id:release.id }]">
<img src="{{release.image}}" alt="Image release">
<h3>{{release.name}}</h3>
<span>{{release.year}}</span>
</div>
</div>
And I've got json of type:
[
{
"id":"release-1",
"name": "Release1 name",
"image": "./cover1.jpg",
"year": "2014"
},
{
"id":"release-2",
"name": "Release2 name",
"image": "./release2.jpg",
"year": "2015"
}
]
My router:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'releases', component: ReleasesComponent },
{ path: 'distro', component: DistroComponent },
{ path: 'contacts', component: ContactsComponent }
];
The problem is that when I click on any *ngFor generated div I get url like
http://localhost:3000/releases;id=release-1
And I want it to look like
http://localhost:3000/releases/release-1
Didn't manage to find a working solution in Angular2 to me.
There are few small syntax mistakes in the code above, here is the code with the fixes.
Structure:
<div class="releases-component">
<div *ngFor="let release of releases" [routerLink]="['/releases',release.id ]">
<img src="{{release.image}}" alt="Image release">
<h3>{{release.name}}</h3>
<span>{{release.year}}</span>
</div>
</div>
Router:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'releases/:id', component: ReleasesComponent },
{ path: 'distro', component: DistroComponent },
{ path: 'contacts', component: ContactsComponent }
];
Here is a one page similar working example plunkr code: http://plnkr.co/edit/UyNkK9?p=preview,
you can see the url changes on this plunkr url,
http://run.plnkr.co/plunks/UyNkK9/
Here is how the url will look after you click on releases, http://run.plnkr.co/plunks/UyNkK9/releases/release-1
html:
<div class="releases-component">
<div *ngFor="let release of releases"
(click)=onSelect(release)>
<img src="{{release.image}}" alt="Image release">
<h3>{{release.name}}</h3>
<span>{{release.year}}</span>
</div
</div>
routes:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'releases/:id', component: ReleasesComponent },
{ path: 'distro', component: DistroComponent },
{ path: 'contacts', component: ContactsComponent }
];
component:
import {Router} from '#angular/router';
...
...
constructor(private router: Router) {}
onSelect(release): any {
this.router.navigate(['/release', release.id]);
}