-
Failed to register a ServiceWorker: The URL protocol of the current origin ('null') is not supported
使用create-react-app脚手架,npm run build生成的文件并不能直接在本地访问
You shouldn't try to "open" index.html, you need to serve it.
When you build, you get instructions in the terminal telling you how to do it. Have you tried following them?
作者给出的答案是要仔细看打包成功后的控制台提示信息,编译成功后会显示这条信息 The build folder is ready to be deployed. You may serve it with a static server: serve -s build
,我们需要全局安装serve,npm i -g serve
,然后在项目的文件夹下 serve -s build
即可开启一个服务,能够直接访问编译好的项目
-
使用webpack-dev-server服务代理跨域
脚手架已经默认安装了插件
我们在package.json中设置中设置代理的域名即可
"proxy": {
"C1/": {
"target": "http://192.168.4.31:8081/",
"changeOrigin": true
}
}
-
热替换
-
配置引入路径 使其简化
当项目嵌套过深时,引入某个模块时都需要很多 '../../../' 诸如此类的引入路径,在webpack中可以配置引入路径,需要同时配置 webpack.config.dev 与 webpack.config.prod两个文件
const MyResolve = dir => {
return path.join(__dirname, '..', dir)
};
alias: {
'react-native': 'react-native-web',
'@': resolve('src')
}
然后可以告别import httpAxios from '../../../../request/httpAxios';
,而使用import httpAxios from '@/request/httpAxios'
的引入方式
-
关于打包后文件路径的问题
默认是在根目录下访问 可以通过在package.json文件中添加"homepage" : " . "
来改变访问路径
直接更改打包后的index.html是不行的 这样会导致引入文件错误