读取javascript函数中的typescript类成员
这是我的代码:读取javascript函数中的typescript类成员,javascript,jquery,angular,typescript,oop,Javascript,Jquery,Angular,Typescript,Oop,这是我的代码: export class CustomToast implements OnInit { toastTypeGroup: string; message: string; title: string; showDuration= "300"; constructor(_toastTypeGroup: string, _message:string,
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() });
}
}
导出类CustomToast实现OnInit{
toastTypeGroup:字符串;
消息:字符串;
标题:字符串;
showDuration=“300”;
构造函数(\u toastTypeGroup:string,\u message:string,\u title:string){
this.toastTypeGroup=\u toastTypeGroup;
this.message=\u message;
this.title=\u title;
}
恩戈尼尼特(){
**//这就是解决方案**
**var_this=这个**
console.log(this.showDuration);
var toastardemo=函数(){
变量toastr:any=(窗口).toastr;
变量k,m=-1,f=0;
var t=函数(){
变量t,o,
//toastTypeGroup
e=此.toastTypeGroup,
//信息
n=此消息,
//头衔
a=这个标题,
//表演持续时间
i=这个.showDuration,
};
返回{init:function(){t()}}
}();jQuery(document).ready(函数(){toastardemo.init()});
}
}
我的问题是,我想从名为CustomToast的类中读取成员值,该类位于名为var t=function(){…}的JavaScript函数中
我无法在JavaScript函数中获取typescript类成员值。
e、 “toastTypeGroup”成员在我的功能中不可访问
这是一个大问题
提前谢谢。我不确定我是否理解了这个问题,但您是否可以添加
。将(此)
绑定到您对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);
试一试
移动
var _this = this;
到ngOnInit的开头,使用
\u this.showDuration
或使用绑定(this)
不工作!可能是因为它在var toastardemo=function(){..}的内部。你们觉得呢?谢谢大家。我只是把这行“var\u this=this;”移到调用strdemo函数之前。我已经将我的帖子更新为正确的行为,返回{init:()=>{t()}
和jQuery(document).ready(()=>{toastardemo.init()})代码>。简而言之,将每次出现的function()
替换为()=>
。可能是重复的。我只是把这行“var\u this=this;”移到调用strdemo函数之前。我已经把我的帖子更新到正确的行为。谢谢大家。我只是把这行“var\u this=this;”移到调用strdemo函数之前。我已经把我的帖子更新到正确的行为。谢谢大家。我只是把这行“var\u this=this;”移到调用strdemo函数之前。我已经更新了我的帖子,使之符合正确的行为。
var _this = this;