构建后index.html中的相对路径 [英] Relative path in index.html after build

查看:129
本文介绍了构建后index.html中的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个reactjs应用,我用波纹管命令构建了项目

Hello i have a reactjs app, and I build my project with bellow command

npm build

这是我的package.json文件:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"},

构建后,我有包含构建文件和index.html文件的文件夹 但是这个.html中的所有路径都是绝对的,我想使用相对路径构建

after build i have folder with build files and index.html file But all paths in this .html are absolute, i want to build with relative path

例如(index.html): 现在我有:

for example (index.html): now i have:

<script type="text/javascript" src="/static/js/main.af2bdfd5.js"></script>
<link href="/static/css/main.097da2df.css" rel="stylesheet">
<link rel="shortcut icon" href="/favicon.ico">

我想要这个:

<script type="text/javascript" src="./static/js/main.af2bdfd5.js"></script>
<link href="./static/css/main.097da2df.css" rel="stylesheet">
<link rel="shortcut icon" href="./favicon.ico">

推荐答案

// package.json
{
  "name": "your-project-name",
  "version": "0.1.0",
  "homepage": "./", # <--- Add this line ----
  ...
}

再次运行npm run build.

这会将路径更改为./,这是项目的相对路径.

This will change the path to ./, which is the relative path of your project.

这篇关于构建后index.html中的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆