简体   繁体   English

在同一类中调用函数不起作用,打字稿

[英]Calling a function inside same class not working, typescript

I am new to typescript and I am having a problem with calling a function inside the same class: I have a function like this: 我是打字稿新手,在同一个类中调用函数时遇到了问题:我有一个类似这样的函数:

createPost(url: String, data: any) {
    $.ajax({
      url: url + "/" + GLOBAL.REGISTER_URL,
      type: "POST",
      data: data.serialize(),
      success: function() {
        console.log("success");
      },
      error: function(request, status, error) {
        console.log(request.responseText);
        console.log(status);
        console.log(error);
      }
    });
  }

And I am trying to call it here: 我想在这里称呼它:

.on('success.form.bv', function(e) {
        $('#success_message').slideDown("slow"); // Do something ...
        $('#contact_form').data('bootstrapValidator').resetForm();

        // Prevent form submission
        e.preventDefault();

        // Get the form instance
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');

        // XXX reassigning port for testing purposes only
        var result = "https://" + window.location.hostname + ":" + GLOBAL.PORT + "/" + GLOBAL.VERSION + "/rest" + GLOBAL.REGISTER_URL;

        this.createPost(result, $form);
      });

But this is not working, every time when I click the button, I got a error in the browser: 但这是行不通的,每当我单击按钮时,浏览器中都会出现错误:

ERROR TypeError: this.createPost is not a function Stack trace: ../../../../../src/app/register/register.component.ts/RegisterComponent.prototype.ngOnInit/<@ http://localhost:4200/main.bundle.js:839:13 dispatch@ http://localhost:4200/assets/js-lib/jquery-3.1.1.min.js:3:10263 add/q.handle@ http://localhost:4200/assets/js-lib/jquery-3.1.1.min.js:3:8325 trigger@ http://localhost:4200/assets/js-lib/jquery-3.1.1.min.js:4:5806 trigger/<@ http://localhost:4200/assets/js-lib/jquery-3.1.1.min.js:4:6310 each@ http://localhost:4200/assets/js-lib/jquery-3.1.1.min.js:2:2813 each@ http://localhost:4200/assets/js-lib/jquery-3.1.1.min.js:2:1001 trigger@ http://localhost:4200/assets/js-lib/jquery-3.1.1.min.js:4:6289 _submit@ http://localhost:4200/assets/js-lib/bootstrapValidator.js:549:13 validate@ http://localhost:4200/assets/js-lib/bootstrapValidator.js:857:13 _init/<@ http://localhost:4200/assets/js-lib/bootstrapValidator.js:109:21 dispatch@ http://localhost:4200/assets/js-lib/jquery-3.1.1.min.js:3: 错误TypeError:this.createPost不是函数堆栈跟踪:../../../../../src/app/register/register.component.ts/RegisterComponent.prototype.ngOnInit/<@ http: //localhost:4200/main.bundle.js:839:13 dispatch @ http:// localhost:4200 / assets / js-lib / jquery-3.1.1.min.js:3:10263 add / q.handle @ http:// localhost:4200 / assets / js-lib / jquery-3.1.1.min.js:3:8325触发器@ http:// localhost:4200 / assets / js-lib / jquery-3.1.1.min .js:4:5806触发器/ <@ http:// localhost:4200 / assets / js-lib / jquery-3.1.1.min.js:4:6310 each @ http:// localhost:4200 / assets / js -lib / jquery-3.1.1.min.js:2:2813每个@ http:// localhost:4200 / assets / js-lib / jquery-3.1.1.min.js:2:1001触发器@ http:/ /localhost:4200/assets/js-lib/jquery-3.1.1.min.js:4:6289 _submit @ http:// localhost:4200 / assets / js-lib / bootstrapValidator.js:549:13 validate @ http :// localhost:4200 / assets / js-lib / bootstrapValidator.js:857:13 _init / <@ http:// localhost:4200 / assets / js-lib / bootstrapValidator.js:109:21 dispatch @ http:/ /localhost:4200/assets/js-lib/jquery-3.1.1.min.js:3: 10263 add/q.handle@ http://localhost:4200/assets/js-lib/jquery-3.1.1.min.js:3:8325 ../../../../zone.js/dist/zone.js/http://localhost:4200/polyfills.bundle.js:2839:17 onInvokeTask@ http://localhost:4200/vendor.bundle.js:49785:24 ../../../../zone.js/dist/zone.js/http://localhost:4200/polyfills.bundle.js:2838:17 ../../../../zone.js/dist/zone.js/http://localhost:4200/polyfills.bundle.js:2606:28 ../../../../zone.js/dist/zone.js/http://localhost:4200/polyfills.bundle.js:2913:24 invokeTask@ http://localhost:4200/polyfills.bundle.js:3785:9 globalZoneAwareCallback@ http://localhost:4200/polyfills.bundle.js:3803:17 10263 add / q.handle @ http:// localhost:4200 / assets / js-lib / jquery-3.1.1.min.js:3:8325 ../../../../zone.js/ dist / zone.js / http:// localhost:4200 / polyfills.bundle.js:2839:17 onInvokeTask @ http:// localhost:4200 / vendor.bundle.js:49785:24 ../../ .. /../zone.js/dist/zone.js/http://localhost:4200/polyfills.bundle.js:2838:17 ../../../../zone.js/dist/zone .js / http:// localhost:4200 / polyfills.bundle.js:2606:28 ../../../../zone.js/dist/zone.js/http://localhost:4200/ polyfills.bundle.js:2913:24 invokeTask @ http:// localhost:4200 / polyfills.bundle.js :3785:9 globalZoneAwareCallback @ http:// localhost:4200 / polyfills.bundle.js:3803:17

This should be a simple function call, and I don't know what is going on, can anyone help me with this problem? 这应该是一个简单的函数调用,并且我不知道发生了什么,有人可以帮助我解决这个问题吗?

I think your problem is for the scope.. what i can suggest to you is to try something like: 我认为您的问题在于范围..我可以建议您尝试的是:

.on('success.form.bv',(e) => { //<-- use arrow func
        $('#success_message').slideDown("slow"); // Do something ...
        $('#contact_form').data('bootstrapValidator').resetForm();

        // Prevent form submission
        e.preventDefault();

        // Get the form instance
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');

        // XXX reassigning port for testing purposes only
        var result = "https://" + window.location.hostname + ":" + GLOBAL.PORT + "/" + GLOBAL.VERSION + "/rest" + GLOBAL.REGISTER_URL;

        this.createPost(result, $form);
      });

OR WRAP your this like: 或像这样包裹你:

var _that = this;
.on('success.form.bv', function(e) {
        $('#success_message').slideDown("slow"); // Do something ...
        $('#contact_form').data('bootstrapValidator').resetForm();

        // Prevent form submission
        e.preventDefault();

        // Get the form instance
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');

        // XXX reassigning port for testing purposes only
        var result = "https://" + window.location.hostname + ":" + GLOBAL.PORT + "/" + GLOBAL.VERSION + "/rest" + GLOBAL.REGISTER_URL;

        _that.createPost(result, $form);
      });

Do not use function {} with TypeScript. 不要将function {}与TypeScript一起使用。 Instead use () => {} so that the this reference is kept valid. 而是使用() => {}以便this引用保持有效。

Otherwise you have to use (function() {}).bind(this) to keep this . 否则,您必须使用(function() {}).bind(this)来保持this

  .on('success.form.bv', (e) => {
    // ......
    this.createPost(result, $form);
  });

You're running into the problem because of closures. 由于关闭,您遇到了问题。 Normally Typescript and Angular work to make this a rare issue, but....you're using jQuery? 通常,Typescript和Angular可以解决这个问题,但是....您使用的是jQuery吗?

To fix: 修理:

change 更改

createPost(url: String, data: any) {...}

to

createPost = (url: String, data: any) => {...}

The fat arrow or lambda function creates a lexical this which doesn't change scope. 粗箭头或lambda函数创建一个词汇表this ,不会改变作用域。

...On a side note, why use jQuery with Angular 2+? ...另一方面,为什么将jQuery与Angular 2+一起使用? Angular already does so much to capture and expose the DOM API, it seems like shooting yourself in the foot and purposefully overcomplicating things by subverting that with jQuery. Angular在捕获和公开DOM API方面已经做了很多工作,这似乎使自己陷入困境,并有意通过用jQuery颠覆它来使事情复杂化。 Angular does enough to make jQuery completely unnecessary. Angular足以使jQuery完全不必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM