我有一个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函数
发布评论