简体   繁体   English

无法使用共享服务在Angular 2组件之间传递数据

[英]Unable to pass data between Angular 2 components using a shared service

I am passing info from a read component to an edit component. 我正在将信息从读取组件传递到编辑组件。 I used a shared service ( modeled off of this post ) but in the child component the Employee info is null. 我使用了共享服务( 以本文为蓝本 ),但是在子组件中,Employee信息为null。 I think maybe I am unintentionally instantiating the service twice which is why it's empty (?) but not sure where I am doing this. 我想可能是我无意间实例化了两次服务,这就是为什么它为空(?),但不确定在哪里执行此操作。 The lines with //undefined should have the same employee info as the parent (parent is working) //未定义的行应与父级具有相同的员工信息(父级正在工作)

Parent

@Component({
    providers: [EmployeesService]
})

export class EmployeeParentComponent {
    private employee: Employee;

    constructor(
        private employeesService: EmployeesService,
        protected router: Router,
        protected route: ActivatedRoute,) { }

    ngOnInit(): void {

    }
//called from HTML template on Edit button click
    toggleEditMode(employee: Employee) {
        this.employeesService.employee = employee;
        alert(this.employeesService.employee.firstName); //correct name
        this.router.navigate('/editPage')
    }
}

Child 儿童

@Component({
    providers: []
})

export class EditEmployeeComponent {
    constructor(
        private employeesService: EmployeesService
        ) {
        console.info(employeesService.employee); //undefined
    }

    ngOnInit(): void {
        console.info(this.employeesService.employee); //undefined
    }
}

Service 服务

@Injectable()
export class EmployeesService {
    public employee: Employee;
}

The reason is that service instance provided by specific component(you are doing right now) can not be accessed by another component. 原因是特定组件(您现在正在做)提供的服务实例不能被另一个组件访问。

If you want to use a service as a singleton, you should provide it at your @NgModule 's providers array. 如果要将服务用作单例,则应在@NgModule的providers数组中提供它。

So just remove EmployeesService from provider array of EmployeeParentComponent and add it to your @NgModule 's providers array 所以只要删除EmployeesService从供应商阵列EmployeeParentComponent并将其添加到您的@NgModule的供应商阵列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2-使用共享服务在组件之间传递数据 - Angular 2 - Using shared service pass data between components 使用Service在组件angular2之间传递数据 - Using Service to pass data between components angular2 无法使用Angular服务在两个angular 4组件之间传递数据 - Unable to pass data between two angular 4 component using Angular service Angular 7:无法从组件之间共享的服务中获取价值 - Angular 7: Unable to get the value from service which is shared between components 如何使用 BehaviorSubject(角度)在具有共享服务的两个不相关组件之间传递数组 - How to pass an array between two non related components with a shared service using BehaviorSubject (angular) 使用在 Angular 中返回空的共享服务在兄弟组件之间共享数据 - Sharing Data between Sibling Components using Shared Service returning empty in Angular Angular:使用服务在组件之间传递数据 - Angular : passing data between components using service 角度2:使用服务在组件之间共享数据 - Angular 2: Using service to share data between components 使用Angular 2中的服务在组件之间发送数据 - Sending data between components using a service in Angular 2 使用服务 Angular 在组件之间传递数据 - Passing data between components using a service Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM