我已经购买了带有以下几行的react模板,但是我不清楚它的目的是什么,该模板在JS中使用,我想将其更改为Typescript
模板中存在以下几行
if (module.hot) {
module.hot.accept('./dashApp.js', () => {
const NextApp = require('./dashApp').default;
ReactDOM.render(<NextApp />, document.getElementById('root'));
});
}
Run Code Online (Sandbox Code Playgroud)
但是,当重命名为.TS时,出现此错误:
Property 'hot' does not exist on type 'NodeModule'.ts(2339)
Run Code Online (Sandbox Code Playgroud)
该代码真正的作用是什么?用简单的英语
mat*_*tor 22
只是添加到已接受的答案:安装后,@types/webpack-env您可能必须添加"types": ["webpack-env"]到您的tsconfig.jsonin compilerOptions. 直到那时它才终于对我有用。
所以你的tsconfig.json看起来像这样:
{
"compilerOptions": {
...
"types": ["webpack-env"]
},
...
}
Run Code Online (Sandbox Code Playgroud)
此代码与Webpack的热模块更换功能(HMR)相关。module.hot像这样工作:
module.hot.accept(
dependencies, // Either a string or an array of strings
callback // Function to fire when the dependencies are updated
);
Run Code Online (Sandbox Code Playgroud)
因此,您包含的代码将执行以下操作:*当./dashApp.js更新了需求/导入树中所需/导入的模块或其中一个模块的代码时,请重新渲染整个React应用。
hot节点模块上的属性不是标准的,因此是TS错误-确保安装所需的类型定义!npm install --save-dev @types/webpack-env应该可以。
相关读物:
module.hot.accept执行| 归档时间: |
|
| 查看次数: |
802 次 |
| 最近记录: |