Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

create-react-app打包路径问题::>_<::急在线等

遇到了一个比较紧急的问题,第一次在项目中用react-router-dom,部署的时候出错了。

create-react-app生成的项目,我在项目中用了react-router-dom, 打包的时候在package.json中尝试配置了homepage的值,设置成以下四种都没用,

  • "homepage": ".",
  • "homepage": "/",
  • "homepage": "http://mywebsite.com/relativepath",
  • "homepage": "http://mywebsite.com/relativepath/",

访问http://mywebsite.com/relativepath显示如下:
image.png
image.png

dependencies

"dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "antd-mobile": "^2.3.4",
    "babel-plugin-import": "^1.13.3",
    "customize-cra": "^1.0.0",
    "react": "^17.0.1",
    "react-app-rewired": "^2.1.8",
    "react-dom": "^17.0.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.1",
    "web-vitals": "^0.2.4"
  },

src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './styles/index.css';
import Main from './pages/Main';
import 'antd-mobile/dist/antd-mobile.css';

ReactDOM.render(
  <React.StrictMode>
    <Main />
  </React.StrictMode>,
  document.getElementById('root')
);

src/pages/main

import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom'
import Index from './Index'
import Detail from './Detail'

function Main() {
  return (
    <Router>
      <Route path="/" exact component={Index} />
      <Route path="/detail/:type" exact component={Detail} />
    </Router>
  )
}

export default Main

官方文档


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你这是阿里云OSS报文件不存在的错误,和react没有关系,你到oss上面取找一下看能不能找到文件,我记得OSS默认不会找文件夹下的index.html, 需要配置


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...