android - Cordova - 路由在 React 应用程序中不起作用
问题描述
我有一个现有的 React 应用程序,我正在尝试将它移植到 iOS 和 Android。
我设法在 iOS 上运行应用程序,只需从常规路由更改为哈希路由。
import { createBrowserHistory, createHashHistory } from 'history';
import { IS_HYBRID_APP, IS_PROD } from 'utils/environment';
export const history = window.cordova ? createHashHistory() : createBrowserHistory();
const connectRouterHistory = connectRouter(history);
但是,这种方法在 Android 上不起作用。导航坏了。
有什么建议吗?
语境:
我所做的步骤是:
环境设置:
npm install -g cordova
- 已安装 Java 8(不支持大于 8 的版本)
- 安装的 Android Studio
- 将 JAVA_HOME、ANDROID_HOME 和 GRADLE_HOME 添加到我的 PATH — 将模拟器添加到路径
- 安装了正确的 sdk 执行:sdkmanager “platform-tools” “platforms;android-27”</li>
- 在 Android Studio 中添加了一个模拟器
应用程序准备和启动:
npm install
cordova platform add android
rm -rf www
react-app-rewired build
// vvv - injects the cordova script and the CSP meta tags into build/index.html
node config/hybrid-app-build.js
mv build www
cordova start android
在模拟器和真实设备上的结果相同。路由无法正常工作,而其他一切似乎都正确。
解决方案
如果您<Router>
在项目中使用,请将其更改为,<HashRouter>
否则您将看到空白显示,因为屏幕上不会呈现任何内容。适用于 iOS 和 Android。
资源:
推荐阅读
- c - 有没有办法在调用 MakeWS2811() 后修改 rpi-ws281x-go 库中 LED 的亮度?
- python - 应用将列用作具有条件的列上的输入的函数
- c++ - 包装成员函数调用的模板类的行为
- hyperledger-fabric - 你能杀死 Hyperledger Fabric 中的智能合约吗
- apache-kafka - Kafka 连接(接收器文件未更新)
- c - 如何为 x86 汇编语言选择正确的源代码
- javascript - MathJax 并不总是使用 Vue 过滤列表呈现
- ruby - Ruby/Minitest Spec:如何测试方法是否返回给定数组中的值?
- azure - Azure 构建管道无法检索最新的源版本
- firebase - 无法检索 Firebase 数据