简体   繁体   English

Reactjs-router-dom和cordova始终直接指向file:///android_asset/www/index.html

[英]Reactjs-router-dom and cordova always direct to file:///android_asset/www/index.html

Ok so I've been following this method and attempting to create a reactjs application combined with cordova , followed every steps on this link and it was working fine but when I try to use react-router-dom every time I run my app on my mobile it doesn't load the correct component instead it loads a path file:///android_asset/www/index.html 好的,所以我一直遵循此方法,并尝试创建与cordova结合使用的reactjs应用程序,按照此链接上的每个步骤进行,并且工作正常,但是当我每次在我的应用程序上运行我的应用程序时都尝试使用react-router-dom移动设备,它不会加载正确的组件,而是会加载路径file:///android_asset/www/index.html

So my App.js is below 所以我的App.js在下面

import React, { Component } from "react";
import { BrowserRouter, Router, Route, Switch } from "react-router-dom";
import Login from "./components/Login";
import Register from "./components/Register";
import Error from "./components/Error";
import Forgot from "./components/Forgot";
import Main from "./components/Main";

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <div>
          <Switch>
            <Route path="/" component={Main} exact />
            <Route path="/login" component={Login} />
            <Route path="/register" component={Register} />
            <Route path="/Forgot" component={Forgot} />
            <Route component={Error} exact />
          </Switch>
        </div>
      </BrowserRouter>
    );
  }
}

export default App;

And my index.js is below importing the App.js 我的index.js在导入App.js下面

import React from "react";
import ReactDOM from "react-dom";
import "./css/main.css";
import App from "./App";
import registerServiceWorker from "./registerServiceWorker";

const startApp = () => {
  ReactDOM.render(<App />, document.getElementById("root"));
  registerServiceWorker();
};

if (window.cordova) {
  document.addEventListener("deviceready", startApp, false);
} else {
  startApp();
}

I updated my code to use HashRoute instead of BrowserRoute 我更新了代码以使用HashRoute而不是BrowserRoute

import React, { Component } from "react";
import {
  BrowserRouter,
  Router,
  Route,
  Switch,
  HashRouter
} from "react-router-dom";
import Login from "./components/Login";
import Register from "./components/Register";
import Error from "./components/Error";
import Forgot from "./components/Forgot";
import createHashHistory from "history/createHashHistory";

let history = createHashHistory();

class App extends Component {
  render() {
    return (
      <HashRouter history={history}>
        <div>
          <Switch>
            <Route path="/" component={Login} exact />
            <Route path="/login" component={Login} />
            <Route path="/register" component={Register} />
            <Route path="/Forgot" component={Forgot} />
            <Route component={Error} exact />
          </Switch>
        </div>
      </HashRouter>
    );
  }
}

export default App;

And here is my component Register which generate the error. 这是产生错误的我的组件Register

import React, { Component } from "react";
import Footer from "./Footer";

class Register extends Component {
  render() {
    return (
      <div>
        <div className="container-login">
          <div className="login-content">
            <div className="login-form">
              <img
                src="img/long logo.png"
                alt="main_log"
                className="img-responsive"
              />

              <div className="input-container">
                <i className="fa fa-user icon" />
                <input
                  className="input-field"
                  type="email"
                  autocomplete="off"
                  placeholder="Full Name"
                  name="email_user"
                />
              </div>
              <div className="input-container">
                <i className="fa fa-envelope icon" />
                <input
                  className="input-field"
                  type="email"
                  autocomplete="off"
                  placeholder="Email"
                  name="email_user"
                />
              </div>
              <div className="input-container">
                <i className="fa fa-key icon" />
                <input
                  className="input-field"
                  type="password"
                  placeholder="Password"
                  name="password"
                />
              </div>
              <div className="input-container">
                <i className="fa fa-key icon" />
                <input
                  className="input-field"
                  type="password"
                  placeholder="Repeat password"
                  name="password"
                />
              </div>
              <div className="input-container">
                <button>Register</button>
              </div>
              <div className="option-section">
                <p>
                  <span>
                    <a href="/forgot">Forgot Password / </a>
                  </span>
                  <span>
                    <a href="/login">Login</a>
                  </span>
                </p>
              </div>
            </div>
          </div>
        </div>
        <Footer />
      </div>
    );
  }
}

export default Register;

Some suggestions said that cordova only serve local files and react-router-dom must be on a server but I really need to know if this is possible to do? 一些建议说, cordova仅提供本地文件,而react-router-dom必须在服务器上,但我真的需要知道是否可以这样做? any suggestion would be great. 任何建议都很好。

It seems that you have to use a HashRouter instead of BrowserRouter : 看来您必须使用HashRouter而不是BrowserRouter

import { HashRouter } from "react-router-dom";

Also here you are some useful links about this theme: Cordova Apps with React , Basics of React Router v4 . 同样在这里,您还可以找到有关此主题的一些有用链接: 带有React的Cordova应用程序 ,React Router v4的基础知识

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的ReferenceError:在文件:///android_asset/www/index.html:27上未定义show_pic - Uncaught ReferenceError: show_pic is not defined at file:///android_asset/www/index.html:27 未捕获的TypeError:对象#<Object>在file:///android_asset/www/index.html上没有方法'exec' - Uncaught TypeError: Object #<Object> has no method 'exec' at file:///android_asset/www/index.html 在phonegap中的file://android_asset/www/index.html上未定义函数 - function is not defined at file://android_asset/www/index.html in phonegap 应用程序错误是一个目录(file:///#android_asset/www/index.html) - Application Error Is a directory (file:///#android_asset/www/index.html) 与服务器的连接不成功。(file:///android_asset/www/index.html) - The connection to the server was unsuccessful.(file:///android_asset/www/index.html) Phonegap:对象xx在文件中没有方法&#39;exec&#39;:///android_asset/www/cordova.js - Phonegap: Object xx has no method 'exec' at file:///android_asset/www/cordova.js “初始化网络连接时出错:找不到类”,来源:file:///android_asset/www/cordova.js - “Error initializing Network Connection: Class not found”, source: file:///android_asset/www/cordova.js Cordova s​​vg <image xlink:href/> throws加载资源失败:net :: ERR_FILE_NOT_FOUND [file:/// android_asset / www / undefined] - Cordova svg <image xlink:href/> throws Failed to load resource: net::ERR_FILE_NOT_FOUND [file:///android_asset/www/undefined] 您好如何为我提供帮助并解决“ file:///android_asset/www/plugin.js中未定义Cordova.exec()”错误 - hi how can help me and resolve “Cordova.exec() is not defined at file:///android_asset/www/plugin.js” error 更改Cordova项目上默认文件:/// android_asset的路径 - Change path for default file:///android_asset on Cordova project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM