初识vue.jsphp
读音:同view,视图的意思html
vue究竟是什么:一个mvvm框架(库),和angular相似,比较容易上手、小巧vue
mvc思想:module层(数据)和view层(视图)分离,由数据驱动视图,相似的有ios
mvp,mvvm,mv*,mvx等git
官网:http://cn.vuejs.org/github
手册:http://cn.vuejs.org/api/ajax
vue和angular的区别json
vue----简单,易学axios
指令以v-xxx的形式api
它是一片html代码(view层),配合上json数据(module层),而后再new出vue实例(vm层即controller)组成
是有我的维护的项目
适合:移动端项目,小巧灵活
vue的发展势头很猛,github上start数量已经超越angular
angular----上手困难,学习成本高
指令以 ng-xxx的形式
全部属性和方法都挂到$scope身上
angular由google维护
合适: pc端项目
共同点: 不兼容低版本IE
vue基本雏形
angular展现一条基本数据: var app=angular.module('app',[‘注入依赖’]); app.controller('xxx',function($scope){ //C $scope.msg='welcome' }) html: div ng-controller="xxx" {{msg}} vue展现数据: html: <div id="box"> {{msg}} </div> var c=new Vue({ el:'#box', //选择器 class tagName data:{ msg:'welcome vue' } });
经常使用指令
angular:
ng-model ng-controller
ng-repeat
ng-click
ng-show
$scope.show=function(){}
vue:
指令: 扩展html标签功能,(其实就是html元素的属性)
v-model 通常用于表单元素(input) 双向数据绑定
循环:
v-for="name in arr"
{{$index}}
v-for="name in json"
{{$index}} {{$key}}
v-for="(k,v) in json"
事件:
v-on:click="函数"
v-on:click/mouseout/mouseover/dblclick/mousedown.....
new Vue({
el:'#box',
data:{ //数据
arr:['apple','banana','orange','pear'],
json:{a:'apple',b:'banana',c:'orange'}
},
methods:{
show:function(){ //方法注册到实例中
alert(1);
}
}
});
显示隐藏:
v-show=“true/false”
事件
v-on:click/mouseover......
简写的:
@click="" 推荐
事件对象:
@click="show($event)"
事件冒泡:
阻止冒泡:
a). ev.cancelBubble=true;
b). @click.stop 推荐
默认行为(默认事件):
阻止默认行为:
a). ev.preventDefault();
b). @contextmenu.prevent 推荐
键盘:
@keydown $event ev.keyCode
@keyup
经常使用键:
回车
a). @keyup.13
b). @keyup.enter
上、下、左、右
@keyup/keydown.left
@keyup/keydown.right
@keyup/keydown.up
@keyup/keydown.down
.....
属性
v-bind:src="" width/height/title.... 简写: :src="" 推荐 <img src="{{url}}" alt=""> 效果能出来,可是会报一个404错误 <img v-bind:src="url" alt=""> 效果能够出来,不会发404请求(推荐使用bind动态绑定)
class和style:
:class="" v-bind:class="" //能够跟单个类
:style="" v-bind:style=""
:class="[red]" //red是数据,能够跟数组
:class="[red,b,c,d]"
:class="{red:a, blue:false}" //a是数据,能够跟对象
:class="json" //能够跟总体一个json数据
data:{
json:{red:a, blue:false}
}
--------------------------
style:
:style="[c]"
:style="[c,d]"
注意: 复合样式,采用驼峰命名法
:style="json"
模板
{{msg}} 数据更新模板变化
{{*msg}} 数据只绑定一次
{{{msg}}} HTML转义输出html标签
过滤器:过滤模板数据
系统提供一下过滤器:
{{msg| filterA}}
{{msg| filterA | filterB}}
uppercase eg: {{'welcome'| uppercase}}
lowercase
capitalize
currency 钱
{{msg| filterA 参数}} (参数以空格分开)
....
交互
全局调用http和实例调用http
(全局使用)Vue.http.get....../ (实例使用)this.$http.get... $http (相似于jq中的ajax功能)
若是vue想作交互 引入: vue-resouce (vue2.0已不维护,推荐使用axios) get: 获取一个普通文本数据: this.$http.get('aa.txt').then(function(res){ alert(res.data); },function(res){ alert(res.status); }); 给服务发送数据:√ this.$http.get('get.php',{ a:1, b:2 }).then(function(res){ alert(res.data); },function(res){ alert(res.status); }); post: this.$http.post('post.php',{ a:1, b:20 },{ emulateJSON:true }).then(function(res){ alert(res.data); },function(res){ alert(res.status); }); jsonp: https://sug.so.360.cn/suggest?callback=suggest_so&word=a https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=jshow 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); });
this.$http.get()/post()/jsonp()
this.$http({ url:地址 data:给后台提交数据, method:'get'/post/jsonp jsonp:'cb' //cbName });