简体   繁体   English

如何在 Electron + React App 中创建路由

[英]How to create route in Electron + React App

Hello all Js Scripting Family,各位 Js 脚本家族,大家好,

Currently I'm working on Electron + React js project at base level i had created project successfully but now i need to move forward to routing process.目前我正在基础级别的 Electron + React js 项目上工作,我已经成功创建了项目,但现在我需要继续进行路由过程。 but i stuck on it and getting error like below但我坚持下去并收到如下错误

> (node:12411) electron: Failed to load URL:
> file:///opt/lampp/htdocs/electron/employee-management/addemployee with
> error: ERR_FILE_NOT_FOUND (Use `electron --trace-warnings ...` to show
> where the warning was created)

I had follow below method for routes我遵循以下路线的方法

-> install react router -> 安装反应路由器
npm install --save react-router-dom npm install --save react-router-dom

import react router dom in index.js file在 index.js 文件中导入 react router dom

import {HashRouter as Router, Route, Switch} from 'react-router-dom';从'react-router-dom'导入{HashRouter as Router, Route, Switch};

my index.js file我的 index.js 文件

import React from 'react';
import ReactDom from 'react-dom';
import {HashRouter as Router, Route, Switch} from 'react-router-dom';
import {Home} from './components/Home';
import { AddEmployee } from './components/AddEmployee';

import '../public/css/style.css';
ReactDom.render(
    <Router>
        <Switch>
            <Route path="/" component={ Home }>
                <Home />
            </Route>
            <Route exact={true} path="/addemployee" component={ AddEmployee }>   
                <AddEmployee />
            </Route>
        </Switch>
    </Router>,
    document.getElementById('root')
);

and my header file where i had added href nav和我添加了 href 导航的头文件

import React from 'react';
import { Container, Button, Navbar, Nav, NavItem, NavDropdown, MenuItem, Form, FormControl } from 'react-bootstrap';

export const Header = () => {
    return (
        <>
        
            <Navbar bg="dark" variant="dark">
                <div className="container-fluid">
                    <Navbar.Brand href="">Employee Management</Navbar.Brand>
                    <Nav className="me-auto">
                        <Nav.Link href="./addemployee">Add Employee</Nav.Link>
                    </Nav>
                    <Form className="d-flex">
                    <FormControl
                        type="search"
                        placeholder="Search"
                        className="mr-2"
                        aria-label="Search"
                    />
                    &nbsp;&nbsp;<Button className="ml-3" variant="outline-secondary">Search</Button>
                    </Form>
                </div>
            </Navbar>
        
        </>
    )
}

Hope you guys understand my question.希望大家明白我的问题。

and one more things is that i had created this single directory project standalone.还有一件事是我独立创建了这个单目录项目。 first create electron project and after install react and use react component for render files.首先创建电子项目,然后安装反应并使用反应组件来渲染文件。

Thank in advance预先感谢

You are routing through href as far as I can see from the parameter name.就我从参数名称中看到的而言,您正在通过href进行路由。 However you are using a HashRouter .但是,您使用的是HashRouter You should push the hash to the history instead.您应该将哈希推送到历史记录中。

Use a Router Link instead of the link from React Bootstrap, which I believe you can also provide that as a component to the bootstrap component.使用Router Link而不是 React Bootstrap 的链接,我相信你也可以将它作为一个组件提供给 bootstrap 组件。

or:要么:

const history = useHistory();
//...
history.push("/addemployee"); //in the onClick handler

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM