Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度主体()可见,在不同组件中不工作 find-employee.component.ts 员工服务 sidebar.component.html 透视表工资_Javascript_Angular_Typescript_Rxjs_Observable - Fatal编程技术网

Javascript 角度主体()可见,在不同组件中不工作 find-employee.component.ts 员工服务 sidebar.component.html 透视表工资

Javascript 角度主体()可见,在不同组件中不工作 find-employee.component.ts 员工服务 sidebar.component.html 透视表工资,javascript,angular,typescript,rxjs,observable,Javascript,Angular,Typescript,Rxjs,Observable,所以我想做的是,在sidebar.component.ts中,当它侦听find employee事件onSelectionChange()(用于显示隐藏菜单)时,我想将像PivotTableSalaryBoolean这样的sidebar属性的值更改为True 为此,我在我的服务中使用Subject(),并希望在它触发侧栏.component.ts中的onSelectionChange()后,它会侦听该事件,但它的工作方式与我希望的不一样。。。我是否遗漏了一些重要的可观察内容?尝试使用不需要任何初

所以我想做的是,在
sidebar.component.ts
中,当它侦听find employee事件
onSelectionChange()
(用于显示隐藏菜单)时,我想将像
PivotTableSalaryBoolean
这样的sidebar属性的值更改为
True


为此,我在我的服务中使用Subject(),并希望在它触发
侧栏.component.ts中的
onSelectionChange()
后,它会侦听该事件,但它的工作方式与我希望的不一样。。。我是否遗漏了一些重要的可观察内容?

尝试使用不需要任何初始化的重播主题

import {ReplaySubject } from 'rxjs/Rx';
@Injectable()
export class EmployeeService
{
    usersIsSelected = new ReplaySubject<any>(1);
    selected = [];

    constructor(private http: Http, private platformLocation: PlatformLocation, private apiResolver: ApiResolver) {       
        this._baseUrl = apiResolver.GetHumanResourceUrl(platformLocation);
    }

    sendSelected(id: any) {
        this.usersIsSelected.next(id);
    }

    getSelected(): Observable<any> {
        return this.usersIsSelected.asObservable();
    }
}
从'rxjs/Rx'导入{ReplaySubject};
@可注射()
出口级雇员服务
{
usersIsSelected=新的ReplaySubject(1);
选定=[];
构造函数(私有http:http,私有platformLocation:platformLocation,私有apiResolver:apiResolver){
此._baseUrl=apisolver.GetHumanResourceUrl(platformLocation);
}
sendSelected(id:任意){
this.usersIsSelected.next(id);
}
getSelected():可观察{
返回此.usersIsSelected.asObservable();
}
}

首先尝试更改主题初始化:

usersIsSelected : Subject<any> = new Subject<any>();
在需要返回数据的地方,您只需订阅UserSiSelected本身即可:

this.serviceEmployee.getSelected().subscribe(
   (res) => this.someThing = res // and further perform the other activities
);

您是
行为主体
的初始化看起来很奇怪。你在用它自己初始化吗?@RobbyCornelissen抱歉我忘了编辑它,我实际上在使用
Subject()
你在哪里提供服务?你确定这两个组件使用的是同一个服务对象吗?@RobbyCornelisseni我提供服务,在find-employee.module中,然后在app.module中,服务应该在同一个模块中吗?你应该只提供一次。从您描述的内容来看,只有
app.module
中的那一个应该在那里。行为主体的目的是提供初始值,我想,行为或重播主体必须用于反映更改,为清楚起见,请参阅
<div class="list-nav-item" routerLinkActive="active" *ngIf="PivotTableSalaryBoolean">
                    <a routerLink="/payroll/employee-for-pivot-table-salary" class="list-nav-link">
                        <span class="list-nav-icon">
                              <i class="fa fa-adjust"></i>
                          </span>
                        <span class="list-nav-label">Pivot Table Salary</span>
                    </a>
                </div>
import {ReplaySubject } from 'rxjs/Rx';
@Injectable()
export class EmployeeService
{
    usersIsSelected = new ReplaySubject<any>(1);
    selected = [];

    constructor(private http: Http, private platformLocation: PlatformLocation, private apiResolver: ApiResolver) {       
        this._baseUrl = apiResolver.GetHumanResourceUrl(platformLocation);
    }

    sendSelected(id: any) {
        this.usersIsSelected.next(id);
    }

    getSelected(): Observable<any> {
        return this.usersIsSelected.asObservable();
    }
}
usersIsSelected : Subject<any> = new Subject<any>();
this.serviceEmployee.usersIsSelected.next(this.employeeSelect);
this.serviceEmployee.getSelected().subscribe(
   (res) => this.someThing = res // and further perform the other activities
);