使用&;this&;出错转换为Typescript后
Getting an error with "this" after converting to Typescript
我的代码看起来是这样的,但是现在我把它放入Typescript类中,它给了我一个错误,说"不能读取未定义的属性角色"。
this.$http({
method: 'POST',
url: '/Token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: 'grant_type=password&username=' + encodeURIComponent(userName) + '&password=' + encodeURIComponent(password),
})
.success(function (data, status, headers, cfg) {
data.roles.split(':').forEach(function (v) {
this.data.role['is' + v] = true;
v == 'Test'
})
我必须用不同的Typescript来编码我的函数吗?我在success()
中有两个function
,我可以用=>
代替它吗?
在本例中,this
表示运行此代码的类的实例。您完全可以将函数转换为箭头函数(=>
)并省略this
。代码看起来像这样:
this.$http({
method: 'POST',
url: '/Token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: 'grant_type=password&username=' + encodeURIComponent(userName) + '&password=' + encodeURIComponent(password),
})
.success((data, status, headers, cfg) => {
data.roles
.split(':')
.forEach((v) => {
data.role['is' + v] = true;
v == 'Test'
})
});
相关文章:
- javascript中的数组出错
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- Foreach无法在Typescript中工作
- 扩展SVGTextElement时出现Typescript Uncaught TypeError
- 为什么无法在TypeScript中导出类实例
- 如何将TypeScript对象转换为普通对象
- Angular 2.0 with JavaScript or TypeScript?
- 在Meteor项目中安装并包含npm模块后出错
- 解析JSON并从中删除对象会出错
- AngularJS指令出错-无法读取属性'编译'的未定义
- "实例范围”;TypeScript类的getter/setter
- 如何在大型nodeJS代码的基础上逐步引入typescript
- 使用javascript在MVC中查找网格长度时出错
- 从HTML调用typescript文件中的函数
- 尝试调用AngularJS上的函数时出错
- 使用&;this&;出错转换为Typescript后
- 当Node.js的许多文件中存在具有相同名称的顶级const或let时,为什么Typescript会出错
- 从typescript调用Web服务时出错
- 为什么这个 Typescript/jscript 代码中的范围会出错
- 将JavaScript转换为TypeScript时出错