简体   繁体   English

如何重定向到 React.js 中的页面

[英]How to redirect to a page in React.js

I'm trying to redirect the page using JSX <Link /> tag.我正在尝试使用 JSX <Link />标签重定向页面。 Here is my code:这是我的代码:

links.js:链接.js:

<Link to="/kategoriler" className="nav-link">
    categories
</Link>

App.js:应用程序.js:

import React, {Component} from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { Link } from 'react-router'
import links from 'links';
import CategoriesPage from './components/pages/categoriesPage';

class App extends Component {
    render(){
        return (
            
            <Router>
                <Route path = "/" component = {App}>
                    <Route path="kategoriler" component={CategoriesPage} />
                </Route>
                
            </Router>
            
        );
    }
} 


export default App;

It should load the categoriesPage.js , but it does nothing.它应该加载categoriesPage.js ,但它什么也不做。 When I go to http://localhost:3000/kategoriler it shows blank page.当我从 go 到 http://localhost:3000/kategoriler 它显示空白页。 What am I missing?我错过了什么?

  1. I'm pretty sure the <Link> component should be imported from react-router-dom and not react-router我很确定应该从react-router-dom而不是react-router导入<Link>组件
  2. <Route path="kategoriler" component={CategoriesPage} /> you did forgot the slash in the route name like this -> <Route path="/kategoriler" component={CategoriesPage} /> , maybe you can add exact params in the route too? <Route path="kategoriler" component={CategoriesPage} />你确实忘记了这样的路由名称中的斜杠 -> <Route path="/kategoriler" component={CategoriesPage} /> ,也许你可以添加exact参数路线也是?
  3. Maybe you can try to put the 2 routes next to each other instead of nesting it inside the App like this也许您可以尝试将 2 条路线彼此相邻放置,而不是像这样将其嵌套在 App 中
<Route exact path="/" component={App} />
<Route exact path="/kategoriler" component={CategoriesPage} />

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

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