简体   繁体   English

尝试访问 TypeScript 中的静态方法时,类型“typeof UserApiController”上不存在属性“users_index”

[英]Property 'users_index' does not exist on type 'typeof UserApiController' when trying to access static method in TypeScript

I am brand new to TypeScript, and trying to call a function on a class.我是 TypeScript 的新手,并试图在类上调用函数。

In file A:在文件 A 中:

import userAPIController from "./controllers/customer/userAPIController";

userAPIController.users_index();

In file B:在文件 B 中:

export default class UserApiController {

    public users_index(): void {
        User.findAll({
            attributes: ["given_name", "family_name"]
        }).success(function (users: any) {
            console.log(users);
        });
}

I get the error:我收到错误:

Property 'users_index' does not exist on type 'typeof UserApiController'

I've also tried to add an interface:我也尝试添加一个接口:

interface userAPIController {
    users_index: any;
}

With no luck.没有运气。

What am I doing incorrectly?我做错了什么?

UsersApiController is a class, so you'll need to instantiate it before you can call its methods: UsersApiController是一个类,因此您需要先实例化它,然后才能调用其方法:

const controller = new usersApiController();
controller.users_index();

You can read more about using classes here: https://www.typescriptlang.org/docs/handbook/classes.html您可以在此处阅读有关使用类的更多信息: https : //www.typescriptlang.org/docs/handbook/classes.html

The function you've written isn't a isn't a static method, it's an instance method.您编写的函数不是静态方法,而是实例方法。 If you want it to be static, change it to:如果您希望它是静态的,请将其更改为:

static users_index = (): void => {
 // content omitted
}

For more on static properties, see this page有关静态属性的更多信息, 请参阅此页面

Or if it's correct that it's an instance method, use the new operator to create an instance before you try to access it:或者,如果它是一个实例方法是正确的,请在尝试访问它之前使用new运算符创建一个实例:

const controller = new UserApiController();
controller.users_index();

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

相关问题 类型 'typeof @types/p5/index"' 上不存在属性 'noise' - Property 'noise' does not exist on type 'typeof @types/p5/index"' TypeScript static 方法中的属性不存在错误 - Property does not exist error in TypeScript static method Typescript static 方法在 someclass 类型上不存在 - Typescript static method does not exist on type someclass 尝试使用 TypeScript 访问 A-frame 中的 Element.object3D 时出现“类型 'Element' 上不存在属性 'object3D'”错误 - "Property 'object3D' does not exist on type 'Element'" error when trying to access Element.object3D in A-frame with TypeScript 属性'findDOMNode'在'typeof React'类型上不存在 - Property 'findDOMNode' does not exist on type 'typeof React' 类型“ typeof window”上不存在属性“ location” - Property 'location' does not exist on type 'typeof window' 类型“typeof”上不存在属性“utm” - Property 'utm' does not exist on type 'typeof 属性“ transformArticles”在类型“ typeof Article”上不存在 - Property 'transformArticles' does not exist on type 'typeof Article' 类型“typeof TextInput”上不存在属性“propTypes” - Property 'propTypes' does not exist on type 'typeof TextInput' “typeof webcrypto”类型上不存在属性“subtle” - Property 'subtle' does not exist on type 'typeof webcrypto'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM