如何将 vue 变量放在 Laravel 括号内

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39389995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:26:10  来源:igfitidea点击:

How to place vue variable inside a laravel bracket

laravellaravel-5laravel-5.2vue.js

提问by Jearson

I am having problem displaying the vue variable output when using it together with laravel. Below is my code.

与 laravel 一起使用时,我在显示 vue 变量输出时遇到问题。下面是我的代码。

<input type="text" 
               class="form-control search-bar__input" 
               placeholder="Search Courses, Interests, Schools or Institutions" 
               autocomplete="on" 
               autofocus="on"
               v-model="query"
               v-on:keyup="search">
<ul class="list-group">
    <li class="list-group-item" v-for="course in courses">
        <a href="{{ route('courses.show', '{{ course.id }}') }}">@{{ course.title }}</a>
    </li>
</ul> 

In the above code I make used of @{{course.id}}inside the route in the a element. However it seems it is not working. Can anyone help me on this.

在上面的代码中,我@{{course.id}}在 a 元素的路由中使用了。但是,它似乎不起作用。谁可以帮我这个事。

回答by Jonathon

You're mixing up server side and client side code there. You're passing @{{course.id}}as a parameter to a PHP function, so you'll probably be getting a syntax error.

您在那里混淆了服务器端和客户端代码。您将@{{course.id}}作为参数传递给 PHP 函数,因此您可能会遇到语法错误。

You should use Laravel to output your route and then in your HTML/Blade you can output a Vue variable using the @{{ }}syntax.

你应该使用 Laravel 来输出你的路由,然后在你的 HTML/Blade 中你可以使用@{{ }}语法输出一个 Vue 变量。

<a href="{{ route('courses.show') }}?course_id=@{{ course.id }}">@{{ course.title }}</a>

That will generate a URL like http://yourdomain.com/courses/path/and stick ?course_id=on the end of it.

这将生成一个喜欢的 URLhttp://yourdomain.com/courses/path/并坚持?course_id=在它的末尾。

My guess is that you're trying to use a route with a parameter in it, rather than a query string. In which case, you should try something like this (Not tested):

我的猜测是您正在尝试使用带有参数的路由,而不是查询字符串。在这种情况下,您应该尝试这样的操作(未测试):

<a href="{{ route('courses.show', '') }}/@{{ course.id }}">@{{ course.title }}</a>

Note that I have put quotes 'around the second parameter to routeand I have omitted the @since we're hoping to directly output {{ course.id }}and we're in PHP, not Blade.

请注意,我'在第二个参数周围加上了引号route,我省略了@因为我们希望直接输出{{ course.id }}并且我们使用的是 PHP,而不是 Blade。

With any luck your URL will be generated as something like this:

幸运的话,您的 URL 将生成如下所示:

http://yourdomain.com/courses/path/{{ course.id }}