What am i doing wrong here?(Anguar Routing) - html

Screenshot
Hey guys, so i am trying to learn Angular by building a simple application that should show the list of categories and create a new category but i am facing a problem, when i click on a link the search bar doesn't change it stays on the home page..
This is the app.component.html:
<ul class = "navbar-nav">
<li class = "nav-item">
<a routerLink="/categories" class="nav-link" ariaCurrentWhenActive="page" >Liste des catégories</a>
</li>
<li class = "nav-item">
<a routerLink="/create-categories" class="nav-link" ariaCurrentWhenActive="page" >Ajout catégorie</a>
</li>
</ul>
</nav>
<div class = "container">
<router-outlet></router-outlet>
</div>
and this is the app-routing-module.ts:
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import { CategoriesDetailsComponent } from './intefaces/categories-details/categories-details.component';
import { CategoriesListComponent } from './intefaces/categories-list/categories-list.component';
import { CreateCategoriesComponent } from './intefaces/create-categories/create-categories.component';
import { UpdateCategoriesComponent } from './intefaces/update-categories/update-categories.component';
const routes: Routes = [
{path: 'categories', component: CategoriesListComponent},
{path: 'create-categories', component: CreateCategoriesComponent},
{path: '', redirectTo: 'categories', pathMatch: 'full'},
{path: 'update-categories/:id', component: UpdateCategoriesComponent},
{path: 'categories-details/:id', component: CategoriesDetailsComponent}
];
#NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

change this
<a routerLink="/categories" class="nav-link" ariaCurrentWhenActive="page" >Liste des catégories</a>
const routes: Routes = [
{path: 'categories', component: CategoriesListComponent},
{path: 'create-categories', component: CreateCategoriesComponent},
{path: '', redirectTo: 'categories', pathMatch: 'full'},
{path: 'update-categories/:id', component: UpdateCategoriesComponent},
{path: 'categories-details/:id', component: CategoriesDetailsComponent}
];
for this
<a routerLink="categories" class="nav-link" ariaCurrentWhenActive="page" >Liste des catégories</a>
const routes: Routes = [
{path: '', redirectTo: 'categories', pathMatch: 'full'},
{path: 'categories', component: CategoriesListComponent},
{path: 'create-categories', component: CreateCategoriesComponent},
{path: 'update-categories/:id', component: UpdateCategoriesComponent},
{path: 'categories-details/:id', component: CategoriesDetailsComponent}
];

Related

How can I acces a route parameter two parents up in Angular 11?

I would like to know how to access lever two parent routes in angular. For example: How do i get the componentId in AttributeListComponent if i use following routes:
const routes: Routes = [
{path: '', redirectTo: '/components', pathMatch: 'full'},
{path: 'components', component: SideNavComponent,
children: [
{path: ':componentId', component: ComponentTabComponent, children: [
{path: '', redirectTo: 'entities', pathMatch: 'full'},
{path: 'entities', component: EntityListComponent, children: [
{path: ':entityId', component: EntityTabComponent,
children: [
{path: '', redirectTo: 'attributes', pathMatch: 'full'},
{path: 'attributes', component: AttributeListComponent},
{path: 'tasks', component: TaskListComponent}
]}
]},
{path: 'domains', component: DomainListComponent}
]},
]},
{ path: '**', component: PageNotFoundComponent }
];
You can traverse router tree up with parent property. Example:
constructor(private route: ActivatedRoute) {}
...
this.route.parent.parent.parent.queryParams.subscribe(...
Calculate how many parent's you need yourself :). That is based on how many children you have in routes file.

Angular 7 - Load images before changing route

I'm currently working on a portfolio. My intention is to only change the route ("portfolio" and its child route "all" which is displayed immediately) as soon as the images on that specific route are fully loaded. When the route was clicked as progress bar from Angular Materials shall be displayed.
Since I'm a beginner with angular I unfortunately can't provide any code, but my app-routing.module.
Browsing the internet I found a similar question, which I didn't really understand:
https://forum.vuejs.org/t/load-images-before-changing-route/11479
App-Routing.Module:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent},
{ path: 'portfolio', component: PortfolioComponent, children: [
{ path: '', redirectTo: 'all', pathMatch: 'full'},
{ path: 'all', component: AllComponent},
{ path: 'wedding', component: PortfolioweddingComponent},
{ path: 'adventure', component: PortfolioadventureComponent},
]},
{ path: 'book-me', component: BookMeComponent},
{ path: 'book-me/wedding', component: WeddingComponent},
{ path: 'book-me/adventuresession', component: AdventuresessionComponent},
];
#NgModule({
imports: [RouterModule.forRoot(routes, {scrollPositionRestoration: 'enabled'}), RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

How to nest more than 1 level route in Angular route?

i make an dashboard app for manage data using angular 6. but i stuck when i nest more than 1 lazyload route, it's not work, It's seem like can not add more than 1 lazyload route in angular router
My App routing :
const appRoutes: Routes = [
{
path: '',
component: SigninComponent,
pathMatch: 'full'
},
{
path: 'dashboard',
loadChildren: './core/dashboard/dashboard.module#DashboardModule'
}
];
#NgModule({
imports: [
RouterModule.forRoot(appRoutes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule {}
my dashboard route
const dashboardRoutes: Routes = [
{
path: '',
component: DashboardComponent,
children: [
{
path: 'products',
pathMatch: 'full',
loadChildren: './products/products.module#ProductModule'
},
]
}
];
#NgModule({
imports: [RouterModule.forChild(dashboardRoutes)],
exports: [RouterModule]
})
My product route:
const productRoutes: Routes = [
{
path: '',
component: ProductListComponent,
children: [
{
path: ':id',
component: ProductEditComponent
},
{
path: 'addproduct',
component: ProductCreateComponent
}
]
}
];
#NgModule({
imports: [RouterModule.forChild(productRoutes)],
exports: [RouterModule]
})
when i access localhost:4200/dashboard/products/id3 it take an error: can not match any route 'dashboard/products/id3'. I think i wrong some where in routing setup but i cant not find where is an error. Anyone can help me ?
I've create a page for you..Just check and do the changes accordingly. You need to check module path correctly otherwise it should work with no issue. You can change the url to hello.
https://stackblitz.com/edit/angular-lazy-loading-nweyjt
// For eg.
// https://stackblitz.com/edit/angular-lazy-loading-nweyjt/hello/3

Component's html included the last HTML

I have problem with HTML like the images below:
Error :
I clicked a button to move to the page with path: /page/:beecow, the last HTML will show if '/page/:beecow' have an error. The last HTML is 'market' in app-routing
I'm using angular version 4.3.5
// Here is my app-routing
const appRoutes: Routes = [
{
path: '',
component: LandingPageComponent,
pathMatch: 'full'
},
{
path: 'page', // Navigate to here
loadChildren: './page/page.module#PageModule'
},
{
path: 'market',
loadChildren: './market/market.module#MarketModule'
},
{
path: '**', redirectTo: ''
}
];
#NgModule({
imports: [RouterModule.forRoot(appRoutes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
// Here is my page-routing
const routes: Routes = [{
path: '', component: PageComponent,
canActivate: [AuthGuardService],
children: [
{
path: 'upload-product',
component: ProductComponent,
pathMatch: 'full',
data: {
authorities: ['ROLE_STORE'],
pageTitle: 'beecow.store.item.productTitle'
},
canActivate: [RoleGuardService],
canDeactivate: [NavigationService]
},
{
path: ':beecow', // if The component have error, html expanded
component: ManageItemComponent
}
]
}];
#NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class PageRoutingModule { }

Angular 4 - Children route makes entire outlet blank

Whenever i click on this link, it result a blank outlet.
This worked properly Before I add 'contact_content':['contact']} to the link.
menubar.component.html (KaltimMenubarComponent)
<ul>
<a routerLink="/kaltim" routerLinkActive="active">
<li>
<img src="assets/news.svg" alt="news"><span>B e r i t a</span>
</li>
</a>
<a [routerLink]="['/kaltim', { outlets: {'kaltim_content':['about']} }]" routerLinkActive="active">
<li>
<img src="assets/about us.svg" alt="about us"><span>T e n t a n g<br>K a m i</span>
</li>
</a>
<a [routerLink]="['/kaltim', { outlets: {'kaltim_content':['contact'], 'contact_content':['contact']} }]" routerLinkActive="active">
<li>
<img src="assets/contact.svg" alt="contact"><span>K o n t a k</span>
</li>
</a>
</ul>
<a routerLink="" routerLinkActive="active" class="start_page">
<img src="assets/start.svg" alt="start">H A L A M A N<br>U T A M A
</a>
contact.component.html (KaltimContactComponent)
<router-outlet name="contact_content"></router-outlet>
app-routing.module.ts
const routes: Routes = [
{ path: '', component: StartComponent, pathMatch: 'full'},
{ path: 'kalteng', component: KaltengComponent, children: [
{ path: 'contact', component: KaltengContactComponent, outlet: 'kalteng_content' },
{ path: 'contact', component: ContactFormComponent, outlet: 'contact_content' },
{ path: 'about', component: KaltengAboutComponent, outlet: 'kalteng_content', pathMatch: 'full' },
{ path: '', component: KaltengMenubarComponent, outlet: 'kalteng_menubar'},
{ path: '', component: KaltengHomeComponent, outlet: 'kalteng_content', pathMatch: 'full'},
] },
{ path: 'kaltim', component: KaltimComponent, children: [
{ path: 'contact', component: KaltimContactComponent, outlet: 'kaltim_content' },
{ path: 'contact', component: ContactFormComponent, outlet: 'contact_content' },
{ path: 'about', component: KaltimAboutComponent, outlet: 'kaltim_content' },
{ path: '', component: KaltimMenubarComponent, outlet: 'kaltim_menubar'},
{ path: '', component: KaltimHomeComponent, outlet: 'kaltim_content', pathMatch: 'full'},
] },
{ path: '', redirectTo: 'start', pathMatch: 'full'},
{ path: '**', component: PageNotFoundComponent }
];
start.component.html (StartComponent)
<a routerLink="/kaltim" routerLinkActive="active" class="main"></a>
<a routerLink="/kalteng" routerLinkActive="active" class="main"></a>
kaltim.component.html (KaltimComponent)
<main>
<router-outlet name="kaltim_menubar"></router-outlet>
<router-outlet name="kaltim_content"></router-outlet>
</main>
contact-form.component.html (ContactFormComponent)
<!-- Usual Forms -->
<label>...</label>
<input>...</input>
Should've put <app-contact-form> (ContactFormComponent's selector) inside contact.component.html rather than uses the routing for that.