我正在使用React-router,当我点击链接按钮时,它工作得很好,但当我刷新我的网页时,它没有加载我想要的东西。
例如,我在localhost/joblist和一切都很好,因为我到达这里按下一个链接。但如果我刷新网页,我会得到:
Cannot GET /joblist
默认情况下,它不是这样工作的。最初我有我的URL localhost/#/和localhost/#/joblist,他们工作得很好。但我不喜欢这种类型的URL,所以试图删除#,我写道:
Router.run(routes, Router.HistoryLocation, function (Handler) {
React.render(<Handler/>, document.body);
});
这个问题不会发生在localhost/,这个总是返回我想要的。
这个应用程序是单页的,所以/joblist不需要向任何服务器询问任何事情。
我的整个路由器。
var routes = (
<Route name="app" path="/" handler={App}>
<Route name="joblist" path="/joblist" handler={JobList}/>
<DefaultRoute handler={Dashboard}/>
<NotFoundRoute handler={NotFound}/>
</Route>
);
Router.run(routes, Router.HistoryLocation, function (Handler) {
React.render(<Handler/>, document.body);
});
用Laravel在React中实现JavaScript SPA的解决方案
公认的答案是对为什么会发生这样的问题的最好解释。如前所述,您必须同时配置客户端和服务器端。
在你的blade模板中,包括JavaScript绑定文件,确保像这样使用URL外观:
<script src="{{ URL::to('js/user/spa.js') }}"></script>
在路由中,确保将此添加到刀片模板所在的主端点。例如,
Route::get('/setting-alerts', function () {
return view('user.set-alerts');
});
以上是刀片模板的主要端点。现在再添加一条可选路线,
Route::get('/setting-alerts/{spa?}', function () {
return view('user.set-alerts');
});
发生的问题是,首先加载刀片模板,然后加载React路由器。当你加载'/setting-alerts'时,它会加载HTML内容和JavaScript代码。
但是当你加载'/setting-alerts/about'时,它首先在服务器端加载。因为它在服务器端,所以这个位置上没有任何东西,并且它返回not found。当你有可选的路由器时,它加载同一个页面,React路由器也被加载,然后React加载器决定显示哪个组件。
路由器可以用两种不同的方式调用,这取决于导航是发生在客户端还是服务器上。您已经将其配置为客户端操作。关键参数是run方法的第二个参数,即location。
当你使用React Router Link组件时,它会阻塞浏览器导航并调用transitionTo来做客户端导航。您正在使用HistoryLocation,因此它使用HTML5历史API通过在地址栏中模拟新URL来完成导航的错觉。如果您使用的是较旧的浏览器,这将不起作用。您将需要使用HashLocation组件。
When you hit refresh, you bypass all of the React and React Router code. The server gets the request for /joblist and it must return something. On the server you need to pass the path that was requested to the run method in order for it to render the correct view. You can use the same route map, but you'll probably need a different call to Router.run. As Charles points out, you can use URL rewriting to handle this. Another option is to use a Node.js server to handle all requests and pass the path value as the location argument.
例如,在Express.js中,它看起来是这样的:
var app = express();
app.get('*', function (req, res) { // This wildcard method handles all requests
Router.run(routes, req.path, function (Handler, state) {
var element = React.createElement(Handler);
var html = React.renderToString(element);
res.render('main', { content: html });
});
});
请注意,正在传递请求路径以运行。为此,需要有一个服务器端视图引擎,可以将呈现的HTML传递给该引擎。在使用renderToString和在服务器上运行React时,还有许多其他注意事项。一旦页面在服务器上呈现,当你的应用程序在客户端加载时,它将再次呈现,并根据需要更新服务器端呈现的HTML。
对于使用iis10的用户,您应该这样做来纠正错误。
确保你使用了browserHistory。作为参考,我会给出路由的代码,但这不是最重要的。重要的是下面组件代码之后的下一步:
class App extends Component {
render() {
return (
<Router history={browserHistory}>
<div>
<Root>
<Switch>
<Route exact path={"/"} component={Home} />
<Route path={"/home"} component={Home} />
<Route path={"/createnewproject"} component={CreateNewProject} />
<Route path={"/projects"} component={Projects} />
<Route path="*" component={NotFoundRoute} />
</Switch>
</Root>
</div>
</Router>
)
}
}
render (<App />, window.document.getElementById("app"));
由于问题是IIS从客户端浏览器接收请求,它将把URL解释为请求一个页面,然后返回一个404页面,因为没有任何可用的页面。做以下几点:
打开IIS
展开“服务器”,然后打开“站点文件夹”
点击网站/应用程序
进入错误页面
在列表中打开404错误状态项
将“将静态文件中的内容插入到错误响应中”选项改为“在此站点上执行URL”,并在URL中添加“/”斜杠值。
现在可以正常工作了。
这可以解决你的问题。
在生产模式下的React应用程序中,我也遇到了同样的问题。这里有两个解决这个问题的方法。
解决方案1。将路由历史更改为“hashHistory”,而不是browserHistory
<Router history={hashHistory} >
<Route path="/home" component={Home} />
<Route path="/aboutus" component={AboutUs} />
</Router>
现在使用命令构建应用程序
sudo npm run build
然后将构建文件夹放在var/www/文件夹中。现在,在每个URL中添加#标签,应用程序就可以正常工作了。就像
localhost/#/home
localhost/#/aboutus
解决方案2:没有#标签使用browserHistory,
在路由器中设置你的history = {browserHistory}。现在使用sudo npm run build构建它。
您需要创建“conf”文件来解决404 not found页面。conf文件应该是这样的。
打开终端,输入以下命令
cd /etc/apache2/sites-available
ls
nano sample.conf
在其中添加以下内容
<VirtualHost *:80>
ServerAdmin admin@0.0.0.0
ServerName 0.0.0.0
ServerAlias 0.0.0.0
DocumentRoot /var/www/html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html/">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
现在您需要使用以下命令启用sample.conf文件:
cd /etc/apache2/sites-available
sudo a2ensite sample.conf
然后,它将要求您重新加载Apache服务器,使用
sudo service apache2 reload or restart
然后打开您的localhost/build文件夹,并添加包含以下内容的.htaccess文件。
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ / [L,QSA]
现在应用程序运行正常。
注意:0.0.0.0 IP地址修改为本地IP地址。
下面是我发现的一个前端解决方案,不需要修改服务器上的任何内容。
假设你的网站是mysite.com,你有一个到mysite.com/about的React Route。
在index.js中,你可以挂载你的顶级组件,你可以像这样放另一个Router:
ReactDOM.render(
<Router>
<div>
<Route exact path="/" component={Home} />
<Route exact path="/about"
render={(props) => <Home {...props} refreshRout={"/about"}/>}
/>
</div>
</Router>,
我假设原始路由器位于虚拟DOM的顶级组件之下。如果你正在使用Django,你还必须在你的。url中捕获url:
urlpatterns = [
path('about/', views.index),
]
不过,这将取决于您使用的后端。请求mysite/about会让你进入index.js(在那里你挂载顶级组件),在那里你可以使用Route的渲染道具,而不是组件道具,并将'/about'作为道具传递给Home组件,在这个例子中。
在Home中,在componentDidMount()或useEffect()钩子中,执行以下操作:
useEffect() {
//check that this.props.refreshRoute actually exists before executing the
//following line
this.props.history.replace(this.props.refreshRoute);
}
我假设你的Home组件是这样渲染的:
<Router>
<Route exact path="/" component={SomeComponent} />
<Route path="/about" component={AboutComponent} />
</Router>
关于如何在路由中向组件传递道具,请参考(将道具传递给React路由器渲染的组件)。