@GivenCui
        
        2017-06-05T15:11:39.000000Z
        字数 5108
        阅读 899
    Vue
一片html代码配合上json,在new出来vue实例
var vm = new Vue({el:'#box', // 选择器data:{ // 数据 (属性)arr:['apple','banana','orange','pear'],json:{a:'apple',b:'banana',c:'orange'}},methods : { // 方法show : function() { console.log(123) }}});
Vue 实例暴露了一些有用的实例属性与方法。这些属性与方法都有前缀 $
var vm = new Vue({el: '#example',data: {a : 123,b : 'test'}})vm.$data === data // -> truevm.$el === document.getElementById('example') // -> true// $watch 是一个实例方法vm.$watch('a', function (newVal, oldVal) {// 这个回调将在 `vm.a` 改变后调用})
/* ============v-for============*/<ul><li v-for="value in arr">{{value}} {{$index}}</li></ul><ul><li v-for="value in json">{{value}} {{$index}} {{$key}}</li></ul><ul><li v-for="(k,v) in json">{{k}} {{v}} {{$index}} {{$key}}</li></ul>
技术要点: Bootstrap
总结: Vue是以数据驱动view, 业务逻辑在于改变数据,几乎用不到dom操作
<!-- data-toggle:弹出, data-target:弹出对象 --><button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer">删除</button><!-- 模态框 弹出框 --><!-- data-dismiss: 关闭自身 id对应data-target --><div role="dialog" class="modal fade bs-example-modal-sm" id="layer"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span>×</span></button><h4 class="modal-title">确认删除么?</h4></div><div class="modal-body text-right"><button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button><button data-dismiss="modal" class="btn btn-danger btn-sm">确认</button></div></div></div></div>
v-if适合第一次渲染的情况, 或变动小的情况
v-show适合显示,隐藏;频繁切换的情况
<tr v-show="myData.length!=0"><td colspan="4" class="text-right"><button class="btn btn-danger btn-sm">删除全部</button></td></tr><tr v-show="myData.length==0"><td colspan="4" class="text-center text-muted"><p>暂无数据....</p></td></tr>
技术要点: vue-resource.js (ajax的get,post,jsonp等)
总结: 客户端通过get,post,jsonp等方式与服务器数据交互(服务器进行数据库的CURD操作)
// 精华:<li v-for="value in myData" :class="{gray:$index==now}">{{value}}</li>
new Vue({el:'body',data:{myData:[],t1:'',now:-1},methods:{get:function(ev){// 排除上下键时出发多余的请求// 太经典了if(ev.keyCode==38 || ev.keyCode==40)return;// 回车选中后跳转if(ev.keyCode==13){window.open('https://www.baidu.com/s?wd='+this.t1); // 打开新窗口跳转// location.href = 'https://www.baidu.com/s?wd='+this.t1; // 原页面跳转this.t1='';}// jsonp请求部分this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{wd:this.t1},{jsonp:'cb'}).then(function(res){this.myData=res.data.s;},function(){});},changeDown:function(){this.now++;if(this.now==this.myData.length)this.now=-1; // 最后一个调到最上面this.t1=this.myData[this.now]; // this.myData[-1] = undefined},changeUp:function(){this.now--;if(this.now==-2)this.now=this.myData.length-1;this.t1=this.myData[this.now];}}});<!-- HTML --><div id="box"><input type="text" v-model="t1" @keyup="get($event)" @keydown.down="changeDown()" @keydown.up.prevent="changeUp()"><!--阻止按上键输入框中的光标移动--><ul><li v-for="value in myData" :class="{gray:$index==now}">{{value}}</li></ul><p v-show="myData.length==0">暂无数据...</p></div>
$ajax和基本http概念 
vue-resource全攻略 
API
/* Response的API */{// Constructorconstructor(any: body, object: {string: url, object: headers, number: status, string: statusText})// Propertiesurl (string)body (any)headers (Headers)ok (boolean)status (number)statusText (string)// Methodsblob() (Promise)text() (Promise)json() (Promise)}
// 基于全局Vue对象使用httpVue.http.get('/someUrl', [options]).then(successCallback, errorCallback);Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);// 在一个Vue实例内使用$httpthis.$http.get('/someUrl', [options]).then(successCallback, errorCallback);this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
/* get:获取数据 *//* 通过get获取本地的a.txt中的内容 */new Vue({el:'body',data:{},methods:{get:function(){this.$http.get('a.txt').then(function(res){alert(res.data);},function(res){alert(res.status);});}}});
/* get:向服务器发送数据 *//* get(url, option)中option为get所传参数 */new Vue({el:'body',data:{},methods:{get:function(){this.$http.get('get.php',{a:1,b:2}).then(function(res){alert(res.data); // 3 (a + b)},function(res){alert(res.status);});}}});/* get.php */<?php$a=$_GET['a'];$b=$_GET['b'];echo $a+$b;?>
new Vue({el: 'body',data: {},methods: {get: function () {this.$http.post('post.php', {a: 1,b: 20}, {headers: {"X-Requested-With": "XMLHttpRequest"},emulateJSON: true // 启用该选项后,请求会以application/x-www-form-urlencoded作为MIME type}).then(function (res) {alert(res.data);}, function (res) {alert(res.status);});}}});/* post.php */<?php$a=$_POST['a'];$b=$_POST['b'];echo $a-$b;?>
- JSONP只支持GET方法, CORS(跨域资源共享)支持GET、POST、PUT、DELETE,但不支持老旧浏览器
 - callback函数的函数名由服务端决定
 - CORS浏览器支持情况可在caniuse.com中查询
 - JSONP相当于GET方法, 所以不用写
 emulateJSON: true
/* 百度输入框输入a,获得的链接简化后得到 *//* 参数解析:wd=a --> "wd":"a"cb=jshow --> cb为回调函数名, jshow随便改对数据无影响*/https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=jshow/* 返回的数据为jshow({q:"a",p:false,s:["爱奇艺","阿里巴巴批发网","airbnb","acfun","阿里云","阿里云邮箱","阿里妈妈","安居客","阿里巴巴","安全教育平台"]});*/new Vue({el:'body',data:{},methods:{get:function(){this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{wd:'a'},{jsonp:'cb' // callback函数的函数名, 默认值为callback,默认值可以省略}).then(function(res){alert(res.data.s);},function(res){alert(res.status);});}}});
