在post函数内部调用的angular 2函数

编程入门 行业动态 更新时间:2024-10-27 11:26:18
本文介绍了在post函数内部调用的angular 2函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个http发布功能.

I have a http post function.

login(){ var data = { email : this.name, password : this.pass, appId : this.appId }; console.log(data); this.http.post(SERVER_URL+"/templatesAuth/authenticateForApp",data) .subscribe(function(res){ let requestParams = { "token": res.token, "email": data.email, "name": res.user.name, "phone": res.user.phone, "streetNumber": res.user.streetNumber, "streetName": res.user.streetName, "country": res.user.country, "city": res.user.city, "zip": res.user.zip, "type": 'internal', "appId":res.user.appId, "registeredUser": res.user.sub }; }, function(err){ alert('login failed'); }) }

在function(req){ }内的

我无法编写或使用examplearray.push();之类的调用 我总是遇到错误无法读取未定义的属性"push"

inside the function(req){ } I cannot write or use calls like examplearray.push(); im always having an error Cannot read property 'push' of undefined

当我使用本地存储功能时.

when I use Local storage functions like.

this.localStorageService.set('test'+this.appId,(requestParams));

错误:无法读取未定义的属性'set'

但是我可以在function(req){ }之外使用它们.无法解决问题

but I can use them out side of the function(req){ }. couldn't figure-out the problem

有什么方法可以将requestParams移到外面.

is there any way to get the requestParams outside.

推荐答案

这是范围界定的非常常见的问题:

This is very common issue of scoping :

只需更改行

.subscribe(function(res){

.subscribe((res) => {

另一种方法是:

Another way of doing is :

.subscribe(function(res){ ... }).bind(this)

要阅读的好文章: toddmotto/es6-arrow-functions-syntaxesand-lexical-scoping/ 并检查以下答案是否为:箭头功能与bind()之间的区别

Great article to read : toddmotto/es6-arrow-functions-syntaxes-and-lexical-scoping/ and check this answer for : Difference between arrow function and bind()

更多推荐

在post函数内部调用的angular 2函数

本文发布于:2023-11-23 03:57:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   post   angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!