我使用reactnet webpack模板创建了一个ReactJS.NET项目,然后将其从React 16升级到最新的v18。然而,当我尝试运行开箱即用的示例时,我会得到以下错误:
ReferenceError: TextEncoder is not defined
at eval (<anonymous>:145:19) -> var textEncoder = new TextEncoder();
at eval (<anonymous>:7002:5)
at ./node_modules/react-dom/cjs/react-dom-server.browser.development.js (vendor.6d1c51ef.js:23:1)
at __webpack_require__ (runtime.36e5d9f0.js:85:30)
at eval (<anonymous>:6:7)
at ./node_modules/react-dom/server.browser.js (vendor.6d1c51ef.js:59:1)
at __webpack_require__ (runtime.36e5d9f0.js:85:30)
at eval (<anonymous>:6:74)
at eval (<anonymous>:47:30)
at ./Content/components/expose-components.js (main.ab0e9b20.js:35:1)
at __webpack_require__ (runtime.36e5d9f0.js:85:30)
at checkDeferredModules (runtime.36e5d9f0.js:46:23)
at webpackJsonpCallback (runtime.36e5d9f0.js:33:19)
at vendor.6d1c51ef.js:1:53
at JavaScriptEngineSwitcher.V8.V8JsEngine.InnerExecute(String code, String documentName)
at JavaScriptEngineSwitcher.Core.JsEngineBase.Execute(String code, String documentName)
at React.JavaScriptEngineFactory.LoadUserScripts(IJsEngine engine)
我该如何解决这个问题?
有两种方法可以尝试:
首先,运行npm install util
,并使用以下代码:
var util= require('util');
const textEncoder = new util.TextEncoder();
第二,尝试运行npm install text-encoding
。
并将此代码粘贴到出现错误的位置:
const TextEncodingPolyfill = require('text-encoding');
Object.assign(global, {
TextEncoder: TextEncodingPolyfill.TextEncoder,
TextDecoder: TextEncodingPolyfill.TextDecoder,
});
const textEncoder = new TextEncoder();
希望这能帮助到你。