📜  Angular:将数据传递给通过路由加载的组件 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:26.878000             🧑  作者: Mango

代码示例1
import {ActivatedRoute, Data} from "@angular/router";

const routes: Routes = [
    {path: 'developer', component: EmployeeComponent, data: {emp_type: 'developer'}},
    {path: 'tester', component: EmployeeComponent, data: {emp_type: 'tester'}},
    {path: 'analyst', component: EmployeeComponent, data: {emp_type: 'analyst'}}
];

class EmployeeComponent {
    constructor(private route: ActivatedRoute) {}

    ngOnInit() {
      this.data_subscription = this.route
        .data
        .subscribe(data: Data => 
          console.log('Employee type passed via route is ' + data.emp_type)
        );
    }

    ngOnDestroy() {
      this.data_subscription.unsubscribe();
    }
}