чтение члена класса машинописи внутри функции javascript - PullRequest
0 голосов
/ 08 мая 2018

Это мой код:

    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 внутри моей JavaScript функции с именем vart = function () {...}.

Я не могу получить значение члена класса машинописного текста внутри JavaScript-функции.например, член "toastTypeGroup" недоступен, кроме моей функции Это большая проблема.

Заранее спасибо.

Ответы [ 3 ]

0 голосов
/ 08 мая 2018

попробуй

/** @type {your type} */
var _this = this;

https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html

0 голосов
/ 08 мая 2018

Ход

var _this = this;

в начале ngOnInit и используйте _this.showDuration или используйте bind(this)

0 голосов
/ 08 мая 2018

Не уверен, что я понял вопрос, но разве вы не добавите .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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...