react 读取 index.html 变量

之前写vue 的时候 可以 在 index.html 里面定义

<script type="text/javascript">
    const dudu = "val"
</script>

然后直接 在 对应的 .vue 文件引用 变量dudu ,

发布了以后 直接 改 index.html 就行

但是在react 里面 这么整就不行了

对应的tsx 文件 是没有办法 import dudu这个变量的,编译都过不去

于是用了个土办法 ,在index.html 这样写

<script type="text/javascript">
    localStorage.setItem("productIp", "172.168.10.52")
</script>

然后在对应的tsx 文件里面 直接 使用 localStorage.getItem("productIp") 就能获取 对应的变量啦

posted @ 2022-05-31 07:45  ifnk  阅读(350)  评论(0编辑  收藏  举报