简体   繁体   English

如何将 Vue.js 变量传递给 Laravel Blade route helper

[英]How to pass Vue.js variable to Laravel Blade route helper

For example, I have a link to an other post like this in my template例如,我的模板中有一个指向其他类似帖子的链接

<a href="{{ route('post', ['post' => @{{ post.id }}]) }}">My post</a>

I want to be able to use the post object within the route helper to use the id.我希望能够在路由助手中使用帖子 object 来使用 id。

Is there any syntax for this use case?这个用例有什么语法吗?

Or is there an other way of doing this?还是有其他方法可以做到这一点?

Actually I had similar problems to solve this kind of cases.实际上我有类似的问题来解决这种情况。 Your question related with Vue exactly, but the method below you can use for your case as well.您的问题与 Vue 完全相关,但您也可以将以下方法用于您的案例。 Anyway you can't execute JS and PHP at the same time, cuz they're working at different sides.无论如何,您不能同时执行 JS 和 PHP,因为它们在不同的方面工作。 But as I also liked to have all routes with their aliases, I thought this approach.. You can imitate something like this:但由于我也喜欢所有路线都带有别名,所以我认为这种方法..你可以模仿这样的东西:

Route::get('/', 'PostController@all')->name('all'); // all posts page
Route::get('post', 'PostController@all')->name('all_page'); // THIS IS THE THING (one additional route), WHICH WILL TAKE RESPONSIBILITY ON CASE, WHEN post_id WILL BE EMPTY
Route::get('post/{post_id}', 'PostController@post')->name('post');

This method will allow you to use the 1st and 3rd routes as normally, and as mixed too in the different places on your app like this:此方法将允许您像往常一样使用第一条和第三条路线,并且在您的应用程序的不同位置也可以混合使用,如下所示:

{{ route('post.all') }}
{{ route('post', ['post' => $post_id]) }}
{{ route('post') }}/@{{ post.id }}

In scripts you can implement the approach like this:在脚本中,您可以实现这样的方法:

let someUrl = "{{ route('post') }}/" + postObj.id;

In the view you can implement the method like this:在视图中,您可以实现如下方法:

<a href="{{ route('post') }}/@{{ post.id }}">My post @{{ post.id }}</a>

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

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