A
A
Anna Gera2019-02-01 01:12:32
JavaScript
Anna Gera, 2019-02-01 01:12:32

How to get a variable from a service in a component?

Good day!
Tell me how to get a variable from a service in a component?
There is a service where the variable is hardcoded:
public getCurrentRealm(metaEntityConfigtime: MetaEntityConfigtime){
const _realmUuid = '578ba27c-21f1-4d38-b686-397d1b1b4ef5';
metaEntityConfigtime.realmUuid = _realmUuid;
};
How to reflect this _realmUuid variable in the component?
Import of service in a component has made.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lideeen, 2019-02-02
@AnnaGera

If I understand you correctly, then the best option is to do so.
Code in service class:

private _realmUuid = '578ba27c-21f1-4d38-b686-397d1b1b4ef5';

public getCurrentRealm(metaEntityConfigtime: MetaEntityConfigtime){
    metaEntityConfigtime.realmUuid = this._realmUuid;
};

//для внешнего использования (в компоненте)
get realmUuid(){
    return this._realmUuid;
}

Then in the component class:
...
constructor(private service: YourService){
    console.log(this.service.realmUuid);
}
...

E
Eugene, 2019-02-01
@zolt85

Write a getter similar to the one you provided for it in the service

getRealmUuid() {
    return '578ba27c-21f1-4d38-b686-397d1b1b4ef5';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question