这是我的代码:
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() });
}
}
我的问题是,我想从名为var t = function(){...}的JavaScript 函数中名为CustomToast 的类读取成员值。< / p>
我无法在 JavaScript函数中获取 typescript类成员值 。
例如&#34; toastTypeGroup&#34;会员是不可访问的是我的fucntion
这是一个大问题。 提前致谢。
答案 0 :(得分:1)
移动
var _this = this;
到ngOnInit的开头并使用_this.showDuration
或使用bind(this)
答案 1 :(得分:0)
我不确定我是否理解了这个问题,但您是否只是将.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);
答案 2 :(得分:0)