• 2017-09-17

笔记及源码地址 : https://github.com/wll8/vue_note

vue 中的事件深入。

事件: @click/mouseover…
事件简写: @ 如 @clkck
属性简写: : 如 :src
传入原生事件对象: $event

  • 事件冒泡:
    原生 ev.cancelBubble=true;
    vue .stop;

  • 默认事件:
    原生 ev.preventDefault();
    vue .prevent;

  • 键盘事件:
    keydown 文字没出现
    keyup 文字出现
    键码 .13
    键名 .enter

  • 属性:
    src=”{{url}}” 废弃
    :src=”url”

  • class和style
    class
    style 复合样式驼峰命名

  • 模板
    数据更新,视图变化。

  • 注,教程失效部分
    属性: src=”{{url}}” 改为 :src=”url”
    单次更新 {{*msg}} 改为 v-once
    解析html {{{msg}}} 改为 v-html

  • 过滤器
    过滤模板数据。 {{‘Hi’ | uppercase}}
    uppercase 转大写
    lowercase 转小写

  • 注,教程失效部分
    所有内置过滤器已经删除。

数据交互

vue想做数据交互,可以使用 vue-resouce。
引入 vue-resouce ,相当于在 vue 实例是挂载了一个 $http 方法。

  • get

this.$http.get('url').then(function(res){
alert(res.data); // 成功
},function(err){
console.log(err); // 失败
})

注,教程失效部分:
在教程中在 $http.get(‘url’,{a:1,b:2}) 形式的参数没有传送成功。
使用 $http.get(‘url?a=1&b=2’) 可以传送成功。

  • post
    post 正常发送数据需要sglf emulateJSON:true 参数。
  this.$http.post('05.post.php',{a:1, b:2, },{
emulateJSON:true // post 需要加些参数才能把参数发送成功
}).then(function(res){
alert(res.data);
},function(err){
console.log(err)
})
  • jsonp
    jsonp 是 get 请求。
    使用 jsonp:'cb' 配置回调用函数。
  this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a',{
jsonp:'cb' // 配置回调函数名,默认 callback
}).then(function(res){
alert(res.data.s);
},function(err){
console.log(err);
})

相关源码

05.数据交互.html

  <meta charset="utf-8">
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
<script src="https://cdn.bootcss.com/vue-resource/1.3.4/vue-resource.js"></script>
<div id="box">
<button @click="f1()">get 获取文本</button> <hr>
<button @click="f2()">get 发送参数</button> <hr>
<button @click="f3()">post 发送参数</button> <hr>
<button @click="f4()">jsonp 360</button> <hr>
<button @click="f5()">jsonp 百度</button> </div>
<script>
var vm=new Vue({
el:'#box',
methods:{
f1:function(){
this.$http.get('05.data.txt').then(function(res){
alert(res.data);
},function(err){
console.log(err);
})
},
f2:function(){
this.$http.get('05.get.php?a=1&b=2').then(function(res){
alert(res.data);
},function(err){
console.log(err)
})
},
f3:function(){
this.$http.post('05.post.php',{
a:1,
b:2,
},{
emulateJSON:true // post 需要加些参数才能把参数发送成功
}).then(function(res){
alert(res.data);
},function(err){
console.log(err)
})
},
f4:function(){
this.$http.jsonp('https://sug.so.360.cn/suggest?word=a').then(function(res){ // jsonp 走 get
alert(res.data.s);
},function(err){
console.log(err);
})
},
f5:function(){
this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a',{
jsonp:'cb' // 配置回调函数名,默认 callback
}).then(function(res){
alert(res.data.s);
},function(err){
console.log(err);
})
},
}
})
</script>

06.百度搜索.html

  <meta charset="utf-8">
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
<script src="https://cdn.bootcss.com/vue-resource/1.3.4/vue-resource.js"></script>
<style>
.cur{
background: #ccc;
}
</style>
<div id="box">
<input v-focus v-model="keyword" @keyup="get_data($event)" @keyup.down="keydown_fn()" @keyup.up.prevent="keyup_fn()">
<div>
<ul>
<li v-for="(item, index) in res_data" :class="{cur:cur_index == index}">{{item}}</li>
</ul>
<p v-show="res_data.length==0">暂无数据...</p>
</div>
</div>
<script>
var vm=new Vue({
el:'#box',
data:{
keyword:'', // 用户输入的关键词
res_data:[], // 返回的结果列表
cur_index:-1, // 当前光标所在的结果上面
},
directives:{
focus:{ // 定义局部指令 v-focus
inserted:function(el){
el.focus(); // 获取焦点
}
}
},
methods:{
get_data:function(ev){
if(ev.keyCode==38 || ev.keyCode==40) return; // 上下键则不发送请求
if(ev.keyCode==13) open('https://www.baidu.com/s?wd='+this.res_data[this.cur_index]); // 回车搜索结果
this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='+this.keyword,{
jsonp:'cb'
}).then(function(res){
this.res_data=res.data.s;
},function(){})
},
keydown_fn:function(){
if(this.cur_index == this.res_data.length) this.cur_index=-1;
this.cur_index++;
this.keyword=this.res_data[this.cur_index];
},
keyup_fn:function(){
if(this.cur_index == -1) this.cur_index=this.res_data.length;
this.cur_index--;
this.keyword=this.res_data[this.cur_index];
},
}
})
</script>

vue.js精讲02的更多相关文章

  1. vue.js精讲01

    笔记及源码地址 : https://github.com/wll8/vue_note 01 2017-09-13 view一个 mvvm框架(库),和 ag 类似.比较小巧,容易上手. mvc: mv ...

  2. Vue 基础精讲

    Vue 基础精讲 Vue 官方文档:https://cn.vuejs.org/v2/guide/ VUE 实例 每个组件都是一个 vue 的实例. 一个 vue 项目是由实例组成的. vue 实例上有 ...

  3. vue.js插件使用(02) vue-router

    概述 vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用.vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来.传统的 ...

  4. vue.js 精学组件记录

    组件需要注册后才可以使用. Vue.component('my-component',{ template:'<div>这是组件内容</div>' }): 局部注册组件 var ...

  5. vue.js 精学记录

    v-bind:class使用的方式: 1.对象与法::class="{'active':isActive}" 此时当isActive为true时,会拥有active 2.数组语法: ...

  6. Vue —— VueX精讲(1)

    大纲 这一讲我们最主要的就是学习vue中的数据管理VueX,这个是一个大杀器 一.回顾一些Promise相关的东西 Promise 有几个比较重要的方法,最重要的还是有一个叫做all的方法,这个也是非 ...

  7. 第三章、vue基础精讲

    3.1VUE实例 组件:全局组件,局部组件,vue的每个组件也是一个实例,有自己的实例属性和实例方法. 在console中调试vue,vm为vue的实例,凡是以$开头的都是vue的实例属性或者vue的 ...

  8. MVVM大比拼之vue.js源码精析

    VUE 源码分析 简介 Vue 是 MVVM 框架中的新贵,如果我没记错的话作者应该毕业不久,现在在google.vue 如作者自己所说,在api设计上受到了很多来自knockout.angularj ...

  9. vue.js源码精析

    MVVM大比拼之vue.js源码精析 VUE 源码分析 简介 Vue 是 MVVM 框架中的新贵,如果我没记错的话作者应该毕业不久,现在在google.vue 如作者自己所说,在api设计上受到了很多 ...

随机推荐

  1. numpy元素级数组函数

    一元函数 abs, fabs 计算整数.浮点数或复数的绝对值.对于非复数值,可以使用更快的fabs. sqrt 计算各元素的平方根.相当于arr ** 0.5 sqare 计算各元素的平方.相当于ar ...

  2. 转:wcf大文件传输解决之道(2)

    此篇文章主要是基于http协议应用于大文件传输中的应用,现在我们先解析下wcf中编码器的定义,编码器实现了类的编码,并负责将Message内存中消息转变为网络发送的字节流或者字节缓冲区(对于发送方而言 ...

  3. AtCoder Beginner Contest 045 B - 3人でカードゲームイージー / Card Game for Three (ABC Edit)

    Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement Alice, Bob and Charlie ...

  4. bzoj4448 情报传递

    题目链接 离线+树上主席树,主席树维护时间标记 注意查询时如果c<0要把c赋为0: #include<iostream> #include<cstdio> #includ ...

  5. [转载]oracle树形查询 start with connect by

    一.简介 在oracle中start with connect by (prior) 用来对树形结构的数据进行查询.其中start with conditon 给出的是数据搜索范围, connect ...

  6. redis的常用命令01

    启动redis的命令: redis-server redis.windows.conf把redis设置成windows下的服务的命令:输入命令后刷新会出现redis的服务:redis-server - ...

  7. printf("loops %u / %u%c[K\n", loops + 1, opts->loops, 27); printf("%cM", 27);

    serialcheck.c中的一段代码一直弄不明白: do { status = stress_test_uart_once(opts, fd, data, data_len); memset(opt ...

  8. php 通过array_merge()和array+array合并数组的区别和效率比较

    众所周知合并两个数组可以使用array_merge(),这是php提供的一个函数.另外还可以通过 array 的方式来合并数组,这两种直接有什么区别,哪一个的效率更高呢? array_merge() ...

  9. 使用v-bind处理class与style

    普通的css引入: 变量引入: 通过定义一个变量fontColor来通过v-bind来进行绑定在h3z的class中 <!--变量引入--> <h3 :class="fon ...

  10. Java中的Volatile和synchronized的区别

    Synchronized和Volatile四个不同点: 1.粒度不同,前者锁对象和类 ,后者针对变量2.syn阻塞,volatile线程不阻塞3.syn保证三大特性,volatile不保证原子性4.s ...