Ron*_*Ron 1 typescript angular-services angular angular5
我在使用Angular 5服务时面临以下问题,请您帮我解决下面的代码有什么问题
我已经解决了以下问题,但没有帮助我
我只想初始化类中的值并在方法中使用它
虽然我已经在类中定义的URL值我得到误差Cannot read property 'url' of undefined为线console.log(this.url);
@Injectable()
export class OwnService {
public accessTokenvalue = '';
public url = 'https://www.someurl.com/id=';
constructor(private hp: HelpService) {
}
public gethpPhotos() {
this.hp.login()
.then(function (response: LoginResponse) {
console.log(response);
console.log(this.url);
})
.catch((error: any) => console.error(error));
}
}
Run Code Online (Sandbox Code Playgroud)
您只需要用function (response: Response)箭头功能替换.每个都function创建它的this内部引用范围.箭头功能将保留this.
.then((response: LoginResponse) => {
console.log(response);
console.log(this.url);
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2394 次 |
| 最近记录: |