javascript - 使用 react-create-wptheme 创建应用程序后路由的 React-router-dom 问题

标签 javascript reactjs wordpress react-router-dom

我的 React 应用程序中的路由问题不大。 我用 npx create-react-wptheme 创建了应用程序. WordPress 是在 mac 上使用 mamp 安装的。

所以,这是我来自 index.js 的代码

import React from 'react'
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import Archive from './templates/Archive';
import Single from './templates/Single';
import Notfound from './templates/Notfound';

const routes = (
   <Router>
       <Switch>
            <Route exact path="/" component={Archive} />
            <Route exact path="/page/:slug" component={Archive} />
            <Route path="/post/:slug" component={Single} />
            <Route path="/search/:term" component={Archive} />
            <Route component={Notfound} />
        </Switch>
    </Router>
);

ReactDOM.render(routes, document.getElementById('root'));

当我在这里添加 basename 参数时: <Router basename="/your-site-wp-edition">

终于第一页开始工作,我可以显示我的索引页。但还有其他页面将我重定向到 404 站点。

我找不到任何解决方案,你能帮我解决这个问题吗?

最佳答案

问题是 wordpress 路由与 react 路由冲突。您需要配置 .htaccess/web-server 以重定向到您的主路径“/”,并让 react 从那里接管。 这通常是 SPA 与服务器一起工作的方式。

您告诉 Web 服务器将所有路由重定向到/。

我对 wordpress 或 php/apache 没有太多经验,但您可以在 SPA htaccess config 中找到更多信息。

关于javascript - 使用 react-create-wptheme 创建应用程序后路由的 React-router-dom 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58895378/

相关文章:

javascript - javascript中许多构造函数参数的替代方法

javascript - 如何禁止某人在 iframe 中加载页面并使用 javascript 提交?

javascript - 对电子邮件进行验证输入,尝试清除错误消息并在单击复选框时将输入变灰

wordpress - 两个 CSS3 投影(左/右和左/右/下)

javascript - Chrome 开发者工具,React 组件不显示组件结构

javascript - 如何检查输入文本字段是否只包含空格?

javascript - 来自 CDN 的 Webpack、React-router 和 React

javascript - React Native 查找文本是否溢出容器

javascript - 联系表格 7 Jquery

html - html文件存储在wordpress插件中的哪里?