vue01

vue01

vue到底是什么?

一个mvvm框架(库)、和angular类似,比较容易上手、小巧

官网:http://cn.vuejs.org/ 
手册: http://cn.vuejs.org/api/

vue和angular区别?

vue——简单、易学
    指令以 v-xxx
    一片html代码配合上json,在new出来vue实例
    个人维护项目

    适合: 移动端项目,小巧

    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(){}
      

      指令: 扩展html标签功能,属性

    • vue

    v-model 一般表单元素(input) 双向数据绑定

循环:v-for

    v-for="name in arr"
        {{$index}}

    v-for="name in json"
        {{$index}}  {{$key}}

    v-for="(k,v) in json"

事件:v-on

    v-on:click="函数"--->简写:@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);
            }
        }
    });

事件对象$event

@click="show($event)"

  • vue1.0

      事件冒泡:
          阻止冒泡:  
              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-show

        v-show=“true/false”

属性绑定:v-bind

v-bind:src="" 简写为:src=""
![]({{url}})    效果能出来,但是会报一个404错误
![](url)    效果可以出来,不会发404请求

class和style:

:class=""   v-bind:class=""
:style=""   v-bind:style=""

:class="[red]"  red是数据 red:'red'
:class="[red,b,c,d]"

:class="{red:a, blue:false}"

:class="json"
    
    data:{
        json:{red:a, blue:false}
    }
--------------------------
style:
:style="[c]"
:style="[c,d]"
    注意:  复合样式,采用驼峰命名法
:style="json"

模板:

{{msg}}     数据更新模板变化
{{*msg}}    数据只绑定一次,数据更新模板不变化
{{{msg}}}   HTML转意输出,可以msg解析数据中标签

过滤器:过滤模板数据

vue1.0系统提供一些过滤器:

{{msg| filterA}}
{{msg| filterA | filterB}}

uppercase   eg: {{'welcome'| uppercase}}
lowercase
capitalize

currency    钱(不传参默认$)
{{12|currency '¥' }}
angular----{{12|currency '¥' }}
{{msg| filterA 参数}}

交互:

如果vue1.0想做交互,需要引入: vue-resouce.js
在组件内的方法里写入:

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);
    });

百度下拉列表案例

    window.onload=function(){
        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);
                        this.t1='';
                    }
                    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];
                },
                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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 初识vue (1) vue到底是什么? 一个mvvm框架(库)、和angular类似 比较容易上手、小巧m...
    懒心丶阅读 310评论 0 0
  • vue复习: vue-resource他会将一个$http属性挂载vm上 $http是定义在vue的原型上,实例上...
    小盼珂阅读 482评论 0 0
  • AngularJS是什么?AngularJs(后面就简称ng了)是一个用于设计动态web应用的结构框架。首先,它是...
    200813阅读 1,668评论 0 3
  • vue 官网 https://cn.vuejs.org/ 下载 1.X 2.X 优点 mvvm的框架 angula...
    米塔塔阅读 532评论 4 8
  • 下午孩子一到家就给我电话,怕妈妈担心,感赏懂得体谅妈妈的乖巧孩子。 孩子一边吃饭,一边跟我分享...
    记得祝福阅读 226评论 0 6