首页 > 解决方案 > 如何在 React js 项目中为文本添加 js 文件?

问题描述

我在项目中有很多文本,所以我只为文本制作了一个 js 文件,并将这个文件添加到 index.html,当使用这个文件时有错误,但是当检查代码时我可以看到文本,

faBundleForms.js

const forms = {
    changeProfitDate: {
        note_1: 'user ',
        note_2: 'add '
    }
};

add to index.html

`<script  src="../static/faBundleForms.js" type="text/javascript">`</script>

use to project

<p>{forms.changeProfitDate.note_1}</p>

我有这个错误

 'forms' is not defined

标签: reactjs

解决方案


如果你真的想使用全局变量(这是个坏主意),你需要window.在它们之前使用,像这样:<p>{window.forms.changeProfitDate.note_1}</p>


推荐阅读