如何将Vue变量放在laravel括号内 [英] How to place vue variable inside a laravel bracket

查看:38
本文介绍了如何将Vue变量放在laravel括号内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与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> 

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

解决方案

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

您应该使用Laravel输出路线,然后在HTML/Blade中使用@{{ }}语法输出Vue变量.

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

这将生成类似http://yourdomain.com/courses/path/的URL,并在其末尾粘贴?course_id=.

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

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

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

如果幸运的话,您的URL将会生成为以下形式:

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

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

<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.

解决方案

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.

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

<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.

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 route and I have omitted the @ since we're hoping to directly output {{ course.id }} and we're in PHP, not Blade.

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

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

这篇关于如何将Vue变量放在laravel括号内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆