温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ajax与vue怎么在laravel 中使用

发布时间:2020-12-28 15:54:26 来源:亿速云 阅读:152 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关ajax与vue怎么在laravel 中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

渲染数据格式:

{{msg}}

但是熟悉Vue渲染的同学知道Vue的格式是:

<div id="app">
 {{ message }}
</div>

这样就引起了冲突,因此,可以是

@{{msg}}

使用@跳出blade引擎模式。

以下代码摘自是laravel-china@leo作者

<tr v-for="item in services">
 <td>@{{ item.id }}</td>
 <td>@{{ item.name }}</td>
 <td>@{{{ displayHosts(item.hosts) }}}</td>
 <td>@{{{ bool2icon(item.enabled) }}}</td>
 <td>@{{{ bool2icon(item.allow_proxy) }}}</td>
 <td>@{{ item.created_at }}</td>
 <td>
  <a href="javascript:void(0)" rel="external nofollow" @click="edit(item)">{{ trans('admin.edit') }}</a>
 </td>
</tr>

以下是本人自己写的代码:

<li class="wrap" v-for="(course,index) in courses.data">
   <div class="img">
    <a v-on:click="show(course)">
    <img v-bind:src="course.thumblink" 
     width="236" height="124">
    </a>
   </div>
   <div class="coursetitle">
   @{{course.name}}
   </div>
   <div class="lecturer">
    讲师:@{{course.teachername}}
    学期:@{{course.semester}}
    
   </div>
   <div class="price">
    @{{course.price}}元
   </div> 
  <div class="pull-left">
  <button type="button" class="btn btn-success btn-xs" v-on:click="show(course)">详情</button>
  </div>

有时候我们可能向带有链接的地方插入Vue数据值作为参数,但是熟悉Vue的同学知道Vue是mustache 语法不能作用在 HTML 特性上,遇到这种情况应该使用 v-bind 指令:

另外一种思路是使用v-on:click。

<a v-on:click="show(course)"> 

Vue实例代码:
methods:{
 show:function(course)
 {
  // debugger;
  window.location.href="/prices/detail/" rel="external nofollow" +course.course_id;
  // console.log(result);
 },
 }

又例如下面的代码:

<img v-bind:src="course.thumblink" width="236" height="124">

就是使用v-bind实现数据的绑定。

最重要的是我吃了很长的时间的惯性思维的亏,认为使用ajax发送请求都是同样的模板。

但是在laravel中必须考虑CSRF-TOKEN。

如下的代码:

 function init_courses()
 {
  // debugger;
  $.ajax({
   type:"post",
   url:'/selectCourse', 
   dataType: 'json',
   data:{result:resul='全部,全部,全部'},
   headers: {
   'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
   },
   success:function(data)
   { 
    mydata.courses=eval(data.result); 
    console.log(mydata.courses);
   },
   error: function(xhr, type){

   alert('Ajax error!')

   }

  });
 };

如果你的页面没有看到一个CSRF,可以在页面头部加入

<meta name="_token" content="{{ csrf_token() }}"/>

这样就可以请求成功。

上述就是小编为大家分享的ajax与vue怎么在laravel 中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI