I am working with an Angular 12 project in Visual Studio 2019 (still a newbee with Angular). I am trying to change a mat-menu-item to navigate to a new component, and I'm getting a Failed to Load Resource error.
The project currently has routing to a component that for this post I've renamed Parent. It lives in: ClientApp/src/app/parent. The code is pretty simple even with the reduced code I'm showing.
In my parent.component.ts file:
import { Component} from '@angular/core';
@Component({
selector: 'parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent {
constructor( ) { }
}
The parent.module.ts file:
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { ParentComponent } from "./parent.component";
@NgModule({
imports: [
RouterModule.forChild([
{
path: 'parent',
component: ParentComponent,
}])
],
declarations: [
....
ParentComponent
]
})
export class ParentModule {}
In app.module.ts (ignore CustomerModule for a moment):
In my app.module.ts
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
.......
import { ParentModule } from "./parent/parent.module";
import { CustomerModule } from "./parent/customer/customer.module";
@NgModule({
declarations: [
AppComponent,
.....
],
imports: [
......
ParentModule,
CustomerModule
],
bootstrap: [AppComponent]
})
export class AppModule {}
My app.component.html references a navigation component, and that contains
<button mat-menu-item [routerLink]="['/parent']">The Parent</button>
And this all works fine, I get navigation to parent. I should add that ParentModule is only referenced in the files I've indicated above. I then added a customer component beneath the parent component and changed routerLink to be
<button mat-menu-item [routerLink]="['/parent/customer']">The Customer</button>
But this produces the 404 error: Failed to load resource: the server responded with a status of 404 (Not Found). Changing routeLink to [routerLink]="['/customer']" produces the same error.
customer is in app.module.ts, see previous. Here is the code (not showing html file) for customer.component.ts file::
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'customer',
templateUrl: './customer.component.html'
})
export class CustomerComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
and for customer.module.ts file:
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { CustomerComponent } from "./customer.component";
@NgModule({
declarations: [CustomerComponent],
imports: [
RouterModule.forChild(
[
{
path: 'customer',
component: CustomerComponent
}]
)
]
})
export class CustomerModule { }
But as I said this does not route when the link is clicked, I only get the 404 error in the browser window, and the VS19 output window says the same. What am I missing that is causing this error? Thanks in advance for any assistance.
EDIT---- I was able to something to work, close to what I want to do, by doing three things:
- Placing my customer component folder in the parent component folder.
- Removing file CustomerModule and its reference in app.module.ts.
- Making the following additions in the parent module:
import { CustomerComponent } from "./parent/customer.component";
....
imports: [
RouterModule.forChild([
{
path: 'parent',
component: ParentComponent,
},
{
path: 'parent/customer',
component: CustomerComponent,
}])
],