Category: "Angular"
Separating the routes in angular
I don't know why the default .net core angular template all routes in the app.modules.ts like this
Code
RouterModule.forRoot([ | |
{ path: '', component: HomeComponent, pathMatch: 'full' }, | |
{ path: 'counter', component: CounterComponent }, | |
{ path: 'fetch-data', component: FetchDataComponent }, | |
]) |
If you build more modules, then the codes will become quite unmanageable. This is not clean at all.
I suggest a better practice, to create app.routes.ts.
This like:
Code
export class AppRoutes { | |
getRoutes() { | |
return [ | |
{ path: '', component: HomeComponent, pathMatch: 'full' }, | |
{ path: 'counter', component: CounterComponent }, | |
| |
{ path: 'fetch-data', component: FetchDataComponent }, | |
]; | |
} | |
} |
Then in the app.modules.ts, it will import the routes
Code
let appRoutes = new AppRoutes(); | |
RouterModule.forRoot(appRoutes.getRoutes()) |
This approach is more clear
[Augular]Push is not defined in the array
I am new to Angular. However, I have used AnuglarJS for a year. That is very hard to catch up. Today, I found a silly error. I cannot add an element to an array by using Pushj function. I got an error,Push is not defined.
students : Person[];
constructor(){
var newPerson = new Person();
this.students.push(newPerson);
}
Finally, I found, I am so silly. The array is not initialized yet.
I need to do that.
students : Person[];
constructor(){
this.students = []
var newPerson = new Person();
this.students.push(newPerson);
}