vue初步认识

vue到底是什么?

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

->1.vue简单、易学    
 指令以v-xxx开头    
 一片html代码配合json,再new出来vue实例  
 由个人维护    
 适合移动端项目      
 vue发展势头很猛 github上star数量已经超越angular
->2.angular上手难           
指令以ng-xxx开头        
所有属性和方法都挂到$scope身上     
由谷歌维护        
更适合PC端项目
共同点 不兼容低版本IE

代码对比

->angular angular展示一条数据

var app = angular.module(‘app’,[]);
app.controller(‘mainController’.function(scope){scope.msg = ‘welcome’; })

html:

 <body ng-app = 'app'></body>>
   <div ng-controller = 'mainController'>
          <div>{{msg}}</div>
   </div>>

->vue

    window.onload = function(){
            var viewmodel = new Vue({
                el:'#box',//告诉你的数据需要展示到那个容器中
                data:{//data中显示你要展示的数据
                    msg:'welcome vue'
                }
            });
        }
<div id="box">
        {{msg}}
    </div>

指令:扩展html标签功能

angular常用指令: ng-model(一般放在表单中) ng-repeat vue常用指令:v-model
循环:v-for=’value in arr’ 或 v-for=’(k,v) in json’ 事件:v-on:click=show();

vue事件深入

->事件时可以简写的 v-on:click=”可以简写成@click=”

事件对象
@click=’show($event,参数)’ 若果改变传参位置则在methods中也要改变
阻止事件冒泡

a、传入$event 方法中通过e.cancelBubble=true方法
b、@click.stop=”

阻止默认行为

a、e.preventDefaul
b、@contextmenu.prevent=’ ’

键盘事件

a、e.keyCode
b、@keydown.键盘码=’show()’
c、@keydown、left(right、top、down) =’show()’

vue属性

v-bind:src=” 可以简写成 :src=”

vue模板

{{msg}}数据更新模板变化
{{*msg}}数据只绑定一次
{{{msg}}html转义输出

过滤器->过滤模板数据

系统提供一些过滤器
uppercase、 lowercase 、capitalize 、currency {{msg|filter}}
{{12|currency ‘¥’}}可以显示 ¥12.00 而angular还需要引入一个国际包

交互

*angular中注入$http
如果vue想做交互,必须引入官方提供的另一个库
vue-resouce
交互的方式:
get

                        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*

                           this.$http.jsonp('https://sug.so.360.cn/suggest?  callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word,obdata&word=s',{
                            word:'s'
                        },{
                            emulateJSON:true
                        }).then(function(res){
                            alert(res.data.s);
                        },function(res){
                            alert(res.status);
                        })
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值