[英]read typescript class member inside javascript function
This is my code : 这是我的代码:
export class CustomToast implements OnInit {
toastTypeGroup: string;
message: string;
title: string;
showDuration= "300";
constructor(_toastTypeGroup: string, _message:string, _title: string ){
this.toastTypeGroup = _toastTypeGroup;
this.message = _message;
this.title = _title;
}
ngOnInit() {
**//this is the solution**
**var _this = this;**
console.log(this.showDuration);
var ToastrDemo = function () {
var toastr: any = (<any>window).toastr;
var k, m = -1, f = 0;
var t = function () {
var t, o,
//toastTypeGroup
e = this.toastTypeGroup,
//message
n = this.message,
//title
a = this.title,
//showDuration
i = this.showDuration,
};
return { init: function () { t() } }
}();jQuery(document).ready(function () { ToastrDemo.init() });
}
}
My problem is that I want to read members values from my class called CustomToast inside my JavaScript function called var t = function () {...}. 我的问题是我想从我的JavaScript函数var t = function(){...}中的CustomToast类读取成员值 。
I can't get typescript class member value inside a JavaScript Function. 我无法在 JavaScript函数中获取Typescript类成员值 。 eg "toastTypeGroup" member is not accessible iside my fucntion This is the big problem. 例如“ toastTypeGroup”成员不可访问,这是我的主要功能。
Thanks in advance. 提前致谢。
Move 移动
var _this = this;
to the beginning of ngOnInit and use _this.showDuration
or use bind(this)
到ngOnInit的开头并使用_this.showDuration
或使用bind(this)
Not sure I understood the question but wouldn't you just add .bind(this)
to your definition of t
? 不确定我是否理解这个问题,但是您是否不就将.bind(this)
添加到t
的定义中?
var t = function () {
var t, o,
//toastTypeGroup
e = this.toastTypeGroup,
//message
n = this.message,
//title
a = this.title,
//showDuration
i = this.showDuration,
}.bind(this);
try 尝试
/** @type {your type} */
var _this = this;
https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.