简体   繁体   English

Laravel路由和使用ReactJS的刀片模板

[英]Laravel routes and blade templating with reactjs

Is there some way I can use blade type syntax with React to handle my Laravel routes? 有什么方法可以在React中使用刀片类型语法来处理我的Laravel路由吗? Let's say I have something like the following: 假设我有以下内容:

<div class="card">
    <a href="{{ route('account.show', $user->slug) }}">Go to your account</a>
</div>

If I try to make this into a Reactjs component, clearly the blade type syntax won't work. 如果我尝试将其放入Reactjs组件中,显然刀片类型语法将不起作用。

I have looked into React Router, but don't exactly know how it would fit into my application, or if it is even necessary for me to use (I'd much rather use plain Laravel routes). 已经研究过React Router,但并不完全知道它如何适合我的应用程序,或者甚至是否需要使用它(我宁愿使用普通的Laravel路由)。

React code is not executed by Laravel, but by the user's browser. React代码不是由Laravel执行,而是由用户的浏览器执行。 So when the time comes to generate that href, inside the user's browser, the code does not have any more access to Laravel or Blade tools. 因此,当需要在用户的浏览器中生成href时,该代码将无法再访问Laravel或Blade工具。

What you can do is to pass all the info you need to your React app, beforehand, when you use blade to generate the top level React invocation: 当您使用blade生成顶级React调用时,您可以做的是事先将所需的所有信息传递给React应用程序:

<script>
    React.render(
        MyApp({
            accountShow: "{{ route('account.show', $user->slug) }}",
            //etc.
        }),
        document.getElementById('myContainer')
    )
</script>

So then in your React app you can use those properties and pass them down to inner components, eg: 因此,在React应用程序中,您可以使用这些属性并将其传递给内部组件,例如:

<CardLink href={this.props.accountShow}>
    Go to your account
</CardLink>

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

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