欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Vite2 + React + Antd 踩坑指南

程序员文章站 2022-05-17 20:13:11
...

process is not defined

报错:

Uncaught ReferenceError: process is not defined

解决方案:

// 使用 import.meta.env 替换 process.env
process.env --> import.meta.env
# .env

# 字段名必须以 VITE_ 开头,否则不会暴露到 import.meta.env 中
VITE_APP_VERSION = "1.0.0"

Property ‘env’ does not exist on type ‘ImportMeta’

报错:

Property 'env' does not exist on type 'ImportMeta'

解决方案:

# tsconfig.json

{
  "compilerOptions": {
    "types": ["vite/client"],
    ...
  }
}       

_interopRequireDefault is not a function

报错:

Uncaught TypeError: _interopRequireDefault is not a function
    at Form.js:24
    at chunk.2VCUNPV2.js?v=9e816b32:4
    at dep:antd_lib_form_Form:1

解决方案:

// useForm 从错误的包导入,导致报错
// import { useForm } from 'antd/lib/form/Form'
// import { useForm } from 'antd/es/form/Form'
// const [form] = useForm()

// 通过 Form 调用,不会出现导包错误的问题
import { Form } from 'antd'
const [form] = Form.useForm()

global is not defined

报错:

Uncaught ReferenceError: global is not defined
    at setImmediate.js:15
    at chunk.2VCUNPV2.js?v=e11687d4:4
    at editOnBeforeInput.js:25
    at chunk.2VCUNPV2.js?v=e11687d4:4
    at DraftEditorEditHandler.js:16
    at chunk.2VCUNPV2.js?v=e11687d4:4
    at DraftEditor.react.js:32
    at chunk.2VCUNPV2.js?v=e11687d4:4
    at Draft.js:24
    at chunk.2VCUNPV2.js?v=e11687d4:4

解决方案:

<!-- index.html -->

<html>
  ...
  <body>
    <script>
      global = globalThis
    </script>
    ...
  </body>
</html>
相关标签: 前端 Vite