【问题标题】:Typescript/React TypeError: Cannot read property 'MaxEmailLength' of undefinedTypescript/React TypeError:无法读取未定义的属性“MaxEmailLength”
【发布时间】:2020-12-20 00:20:05
【问题描述】:

我对 typescript 有一个奇怪的问题,所以我有 2 个静态类互相使用,这不起作用。

类 ValidationHelper(单个文件)

import { ValidationErrors } from '../dictionary/ValidationErrors';
    export class ValidationHelper {
    
        static readonly MaxEmailLength = 320;
    
        static IsEmailValid(email: string): ValidationResult {
            if (email.length < 0) {
                return new ValidationResult(false, ValidationErrors.MissingEmail);
            }
    
            if (email.length > this.MaxEmailLength) {
                return new ValidationResult(false, ValidationErrors.InvalidEmailLength);
            }
    
            return new ValidationResult(true);
        }
    }

类验证错误(单个文件)

import { ValidationHelper } from "../helpers/ValidationHelper";
    export class ValidationErrors {
    
        static readonly MissingEmail = "Please provide email.";
    
        static readonly InvalidEmailLength = `Email can't be longer than ${ValidationHelper.MaxEmailLength} characters.`;
    }

这些类用于反应组件。一切都是由 webpack 4.44.1 构建到 vendor.js runtime.js 和我需要的每个条目的文件中。然后使用&lt;script&gt;标签将这些js文件加载到HTML页面中。

这是我安装的依赖项:

"devDependencies": {
    "@babel/core": "^7.11.1",
    "@babel/plugin-proposal-class-properties": "^7.10.4",
    "@babel/plugin-proposal-object-rest-spread": "^7.11.0",
    "@babel/plugin-transform-runtime": "^7.11.0",
    "@babel/preset-env": "^7.11.0",
    "@babel/preset-react": "^7.10.4",
    "@babel/preset-typescript": "^7.10.4",
    "@typescript-eslint/eslint-plugin": "^3.9.0",
    "@typescript-eslint/parser": "^3.9.0",
    "babel-loader": "^8.1.0",
    "css-loader": "^4.2.1",
    "eslint": "^7.6.0",
    "grunt": "^1.1.0",
    "grunt-contrib-less": "^1.4.1",
    "grunt-contrib-watch": "^1.1.0",
    "jit-grunt": "^0.10.0",
    "less-loader": "^6.2.0",
    "style-loader": "^1.2.1",
    "typescript": "^3.9.7",
    "webpack": "^4.44.1",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0",
    "webpack-merge": "^5.1.1"
  },
  "dependencies": {
    "@babel/runtime": "^7.11.2",
    "@types/react": "^16.9.46",
    "@types/react-dom": "^16.9.8",
    "grunt-contrib-concat": "^1.0.1",
    "grunt-contrib-cssmin": "^3.0.0",
    "grunt-contrib-jshint": "^2.1.0",
    "grunt-terser": "^1.0.0",
    "grunt-webpack": "^4.0.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  }

这是错误: TypeError:无法读取未定义的属性“MaxEmailLength”

作为调查的一部分,我在我的代码中注释掉了很多,我只在 react 组件的 render 方法中留下了这个:

<p>{ValidationHelper.MaxEmailLength}</p>

这不起作用。似乎一个简单的反应组件会因为渲染方法中的那一行而失败。

我还在当前正在运行 typescript/react 的不同项目/解决方案上尝试了这个东西,我遇到了同样的问题(看起来它是这样工作的)。

作为不同测试的一部分,我已经移动了这个

static readonly MaxEmailLength = 320;

分离新的静态类然后它就可以工作了。

看起来这是因为 ValidationHelper 类使用 ValidationErrors 类,然后 ValidationErrors 类使用 ValidationHelper 类。

另外,我可以补充一下,这是 3.9.7 typescript 版本。

我的问题是这应该有效吗?

【问题讨论】:

  • this.MaxEmailLength 应该是 ValidationHelper.MaxEmailLength,因为该字段是静态的。
  • @Clashsoft - 我试过是一样的

标签: reactjs typescript webpack static frontend


【解决方案1】:

这在 TypeScript playground 中对我有用。错误可能是ValidationHelper 在您尝试使用的文件中不可用吗?

仅供参考,我将其发布为答案,因为链接太长,无法放入评论

【讨论】:

  • 所以这些类中的每一个都在一个单独的文件中。在 ValidationHelper 中,我通过 import { ValidationErrors } from '../dictionary/ValidationErrors' 导入;在 ValidationErrors 我通过 import { ValidationHelper } from "../helpers/ValidationHelper";
  • 我也不确定操场是如何工作的,但它似乎不是完整的例子。我的意思是通常我使用 webpack 4.44.1 构建它,并设置为每个我需要的条目生成 vendor.js 和 runtime.js 和文件。然后我在 HTML 页面中使用
  • 游乐场并不重要。它只是表明错误不在代码中,而必须是由于您如何导入/导出模块。因此,如果没有更多相关信息,我们将无法为您提供帮助。
  • 在之前的cmets中我使用import关键字在每个文件中导入模块,然后这些类用于react组件。一切都由 webpack 4.44.1 构建,并在 HTML 页面中使用
猜你喜欢
  • 2018-03-13
  • 2021-10-31
  • 2017-11-02
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2021-09-24
  • 2020-07-10
  • 2019-07-07
相关资源
最近更新 更多