javascript - 无法读取属性 ... undefine ,在 Object.callbackFromNative In angular 2
问题描述
我有一个按流的代码
declare var Speech: any;
export class Lesson implements OnInit {
text = '';
constructor()
{
this.text = 'hh';
}
ToSpeech(){
Speech.listen(this.listenSucess);
}
listenSucess(result){
this.text = reuslt;
}
文件 Speech.js
var Speech= (function () {
return {
listen:funtion(sucess){
window.plugins.speechRecognition.startListening(function (result) {
success(result);
}
}
}
我有一个错误,例如:
Cannot read property 'this.text' of undefined
at Object.callbackFromNative
我为此功能使用了回调,谢谢。
解决方案
您是否尝试过胖箭头=>
而不是function
?因为this
在 a 内部无法访问function
var Speech = (() => {
return {
listen = (success) => {
window.plugins.speechRecognition.startListening((result) => {
success(result);
}
}
}
推荐阅读
- authentication - gRPC 的基于令牌的身份验证是否会为每个调用添加元数据?
- redis - [ApacheBeam RedisIO]:过期时间不适用于 PFADD 写入方法
- python - if __name__ == "__main__": 和 Python 中的递归
- node.js - 用 NODE.js 在变量上多次替换字符串
- c++ - Poco::Foundation Target Not found with conan
- plsql - 无法在 SQL Developer 中运行教程过程:PLS-00103:遇到符号
- java - java - 如何在java中排除百分之五的数字
- python - modbus poll 跳转到异常只有没有错误?
- javascript - 如何限制反应原生谷歌地方自动完成到一个特定的大陆
- android - Android Studio 中 Firebase Crashlytics 的 mappingFileUploadEnabled 是否默认设置为 false?