如何将参数从laravel传递到ReactJS? [英] How to pass params from laravel to ReactJS?
问题描述
我的应用程序使用Lavarel,并且我具有某些ReactJS组件.路由由Laravel完成.对于路由/Users/< userid>
,如果控制器为 UserController.php
且视图为 userview.blade.php
,该怎么办我从我的react组件中的URL中获得了< userid>
,该URL加载在 userview.blade.php
上的div中?
My application uses Lavarel and I have certain ReactJS components. Routing is done by Laravel. For a route /Users/<userid>
, if the controller is UserController.php
and view is userview.blade.php
, how do I get the <userid>
from the URL in my react component loaded in a div on userview.blade.php
?
推荐答案
您可以从Laracasts中试用此库,该库允许您通过Controller中的Facade将服务器端数据(字符串/数组/集合)传递给JavaScript.
You can try out this library from Laracasts which allows you to pass server-side data (string/array/collection) to your JavaScript via a Facade in your Controller.
这可能看起来像这样(还没有测试,但是您了解了要点).
This may look something like this (haven't tested it, but you get the gist).
public function UserController($userid)
{
JavaScript::put([
'userid' => $userid,
]);
return View::make('yourview');
}
文档进一步展示了如何从您的视图访问它.
The docs further show how to access it from your view.
https://github.com/laracasts/PHP-Vars-To-Js-Transformer
这篇关于如何将参数从laravel传递到ReactJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!