vue-resource
来源:互联网 发布:怎样上淘宝买东西 编辑:程序博客网 时间:2024/05/23 12:56
vue-resource
vue-resource文档
安装
npm install vue-resource
引入
像jquery插件要在jquery之后引入一样,vue-resource要在vue之后才引入import VueResource from 'vue-resource';Vue.use(VueResource);
使用
vue-resource的使用方法和jquery的ajax使用方法可以说是一毛一样的。
简直可以把resource就当作ajax来用。
get方法this.$http.get("http://localhost/test.php").then( function (res) { // 处理成功的结果 console.log(res.body); },function (res) { // 处理失败的结果 });post方法this.$http.post("http://localhost/test.php",{name:"zhangsan"},{emulateJSON:true}).then( function (res) { // 处理成功的结果 console.log(res.body); },function (res) { // 处理失败的结果 } );test.php<?php// 指定允许其他域名访问header('Access-Control-Allow-Origin:*');// 响应类型header('Access-Control-Allow-Methods:GET,POST,PUT');header('Access-Control-Allow-Headers:x-requested-with,content-type');var_export($_POST);die('hello');
值得一提的是,自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource。目前来说,VueJS推荐使用axios网络请求处理方案。因为axios属于isomorphic方案,正好和VueJS2支持服务端渲染暗合。
vue执行ajax请求
js代码
var demo = new Vue({ el: '#demo', data: { txt: 1, }, methods: { ajax: function() { var self = this; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { var json = JSON.parse(xmlhttp.responseText) console.log(json) console.log(self.a) self.a = json.name; } } xmlhttp.open("GET", "test.php", true); xmlhttp.send(); } }})
html代码
<p>{{txt}}</p><button @click="ajax()">Ajax</button>
php代码
<?php echo json_encode(array('name'=>'xunyhu'));?>
阅读全文
0 0
- vue-resource
- vue-resource
- vue-resource
- vue vue-resource
- vue 引用 vue-resource步骤
- vue学习之vue-resource
- Vue mock & vue-resource & JSONP
- vue+vue-resource post请求
- vue vue-router vue-resource学习记录
- vue-resource插件使用
- vue-resource跨域
- vue-resource插件使用
- vue-resource插件使用
- vue-resource CRUD示例
- Vue-resource简述
- vue-resource插件使用
- vue-resource插件使用
- 数据交互vue-resource
- AlertDialog跟PopupWindow对话框
- 牛津通识读本:数学(中文版) 蒂莫西·高尔斯笔记1
- JavaWeb小项目涉及的基础知识02
- unittest 中用于 skip 跳过 test method, test class,的相关装饰器
- MDK编译问题
- vue-resource
- SikuliX安装
- C++之函数模板与类模板的区别(三)
- JQuery基础知识
- OpenGL导入3D模型的准备工作
- Android-ViewPager-循环滑动(轮播)
- 2.创建适合游戏的窗口和消息循环
- Makefile常用万能模板(包括静态链接库、动态链接库、可执行文件)
- 动态代理