Reputation: 41
ngOnInit() {
this.activatedRoute.data.subscribe(({ agent }) => {
if (agent) {
console.log(agent);
}
}
I would like to understand where does agent come from because I didn't send agent in the url ? May be I don't really understand the use of activatedRoute.data.suscribe
Upvotes: 0
Views: 43
Reputation: 31105
Angular ActivatedRoute
can accept an argument data
which could be used to send data. It would be resolved to an observable that could then be subscribed to in the component. And as stated in my comment, the snippet uses de-structuring assignment to get only the property agent
from the object.
Example
Routing config
const appRoutes: Routes = [
{ path: "", component: DashboardComponent },
{
path: "editTodo",
data: { agent: "Sample Agent", id: "1" },
component: EditTodoComponent
}
];
@NgModule({
imports: [ BrowserModule, FormsModule, RouterModule.forRoot(appRoutes) ],
declarations: ...,
bootstrap: [ AppComponent ]
})
export class AppModule { }
Component
import { ActivatedRoute } from "@angular/router";
export class EditTodoComponent implements OnInit {
constructor(private _actRoute: ActivatedRoute) {}
ngOnInit() {
this._actRoute.data.subscribe({
next: ({ agent }) => {
if (agent) {
console.log(agent);
}
}
});
}
}
Working example: Stackblitz
Upvotes: 2