vue01

2017-09-15  本文已影响0人  追卓2018

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基本雏形:

循环: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)"

显示隐藏: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>
上一篇 下一篇

猜你喜欢

热点阅读