首页 > 解决方案 > 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
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 

在模拟器和真实设备上的结果相同。路由无法正常工作,而其他一切似乎都正确。

标签: androidreactjscordovamobilehybrid-mobile-app

解决方案


如果您<Router>在项目中使用,请将其更改为,<HashRouter>否则您将看到空白显示,因为屏幕上不会呈现任何内容。适用于 iOS 和 Android。

资源:

https://stackoverflow.com/a/46785362/3850405


推荐阅读