通过服务传递数据时出现角度错误

时间:2019-05-15 07:41:27

标签: angular

我正在通过服务传递数据。 data.ts

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable()
export class Data {
    private urlHistory: string;

    constructor() {
        this.urlHistory = "";
    }

    public setUrlHistoryObj(val: string): void {
        this.urlHistory = val;
    }

    public getUrlHistoryObj(): string {
        return this.urlHistory;
    }
}

login.ts

if(this.loginForm.value.email == 'samad' && this.loginForm.value.password == 'admin') {
    console.log(`found corresponding user.`);
    this.router.navigate(['/dashboard/home', {company_id: '659' , policy: '' }]);
    this.data.setUrlHistoryObj("/movie/searchMovie");

}

employe.ts

constructor(private api:ApiService,private modalService: NgbModal,private router:Router, public httpClient: HttpClient, public data: Data) {
    this.getClaims();
    this.backUrl = this.data.getUrlHistoryObj();
}

我通过该服务传递了一些数字,但在处理它时显示了错误TypeError:this.data.getUrlHistoryObj不是一个函数。但这是data.ts中的功能。有人知道问题出在哪里吗?

2 个答案:

答案 0 :(得分:0)

是否有可能从import { Data } from '@angular/router';中的@ angular / router(employe.ts)而不是您自己的Data中导入数据?

答案 1 :(得分:0)

Your behaviour subject is private make it public so it is accessible