vue01

作者: 追卓2018 | 来源:发表于2017-09-15 01:33 被阅读0次

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>

相关文章

  • Nexus npm install 报错E401

    背景是搭建nexus后,npm install 无法使用[root@es vue01]# npm isntalln...

  • VUE01

    vm是什么? 差值表达式 v-text指令 v-HTML指令 v-bind v-for v-model v-on ...

  • vue01

    添加事件 添加 {{vel}} 删除 new Vue({ el:'.you', data:{ sg:['dd','...

  • Vue01

    一、认识Vue 定义:一个构建数据驱动的 web 界面的渐进式框架 优点: 1、可以完全通过客户端浏览器渲染页面,...

  • vue01

    vue复习: vue-resource他会将一个$http属性挂载vm上 $http是定义在vue的原型上,实例上...

  • vue01

    vue01 vue到底是什么? 一个mvvm框架(库)、和angular类似,比较容易上手、小巧 vue和angu...

  • Vue01

    目录 1.1.认识Vuejs 为什么学习Vuejs 1.vue在国内领域是必备技能。 Vue的渐进式 1.把vue...

  • vue01

    webstrom中常用的快捷键一:标签自动补全:(1)纯标签补全例:输入h1,按Tab键, (2)纯标签+地址“i...

  • VUE01:Vue编程模式

    要使用Vue,顺带做个笔记,无他!  1. 理解Vue的封装对象;  2.Vue的入门使用;  Vue可以直接使用...

  • Vue01组件化实践-03 插槽 slot

    组件化 slot 插槽 demo github地址:feature/slot 分支 直接上代码体会插槽的使用吧

网友评论

      本文标题:vue01

      本文链接:https://www.haomeiwen.com/subject/ozgjsxtx.html