简体   繁体   English

打字稿静态函数未接收参数

[英]Typescript static function isn't receiving arguments

I'm trying to reach a static function from within a Typescript object's public function, but it doesn't seem to work.我试图从 Typescript 对象的公共函数中访问一个静态函数,但它似乎不起作用。

The goal is to implement an Angular-style form validator in Vue.目标是在 Vue 中实现一个 Angular 风格的表单验证器。 The parent component defines an array of ReactiveFormElement objects, which are controllers for the actual form controls.父组件定义了一组ReactiveFormElement对象,它们是实际表单控件的控制器。 These can receive two parameters upon creation: a value and an array of validators.这些可以在创建时接收两个参数:一个值和一个验证器数组。

The validators are static functions in another object.验证器是另一个对象中的静态函数。

Here's a simplified rundown:这是一个简化的纲要:

Parent component:父组件:

import ReactiveFormElement from '@/components/ReactiveFormElement';
import Validators from '@/services/validators';
...

    public formData = {
        title: new ReactiveFormElement('', [Validators.required]),
    };

The reactive form element:反应式表单元素:

export default class ReactiveFormElement {

    constructor(
        public value?: any,
        public validators: Function[] = []) { }

    public validate() {
        console.log('Sending to validator: ' + this.value);
        const error = validator.call(this.value);
    }
}

And the validators look like this:验证器如下所示:

export default class Validators {

    static required(value: any) {
        console.log('Validator received: ', value);
        if (typeof value === 'undefined' || !value)
            return 'This value is required'
        else
            return null;
    }
}

The problem: Whatever I pass to the validator will end up being undefined .问题:我传递给验证器的任何内容最终都将是undefined As you see, I'm printing the value I'm sending from ReactiveFormElement.validate() to Validators.require() to the console.如您所见,我正在将我从ReactiveFormElement.validate()发送到Validators.require()的值打印到控制台。 The output is:输出是:

Sending to validator: Some string
Validator received: undefined

Why is it undefined?为什么是未定义的?

It doesn't help if the validator function isn't static.如果验证器函数不是静态的,那也无济于事。 It doesn't help if I use apply instead of call (putting the value into an array, of course).如果我使用apply而不是call (当然是将值放入数组),这没有帮助。 Interestingly the validator works if I call it from the parent component.有趣的是,如果我从父组件调用它,验证器就可以工作。 But it refuses to work from inside ReactiveFormElement .但它拒绝从ReactiveFormElement内部工作。

The problem is your scoping.问题是你的范围。 If you pass this as a first argument to the call method (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call ) your method will receive the correct input.如果您this作为第一个参数传递给 call 方法(请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call ),您的方法将收到正确的输入。

Working snippet:工作片段:

class ReactiveFormElement {

    constructor(
        public value?: any,
        public validators: Function[] = []) { }

    public validate() {
        console.log('Sending to validator: ' + this.value);
        this.validators.forEach(validator =>
            validator.call(this, this.value));
    }
}

class Validators {

    static required(value: any) {
        console.log('Validator received: ', value);
        if (typeof value === 'undefined' || !value)
            return 'This value is required'
        else
            return null;
    }
}

const formElement = new ReactiveFormElement('foo', [Validators.required]);
formElement.validate();

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

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