• 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. AtCoder Beginner Contest 045 C - たくさんの数式 / Many Formulas

    Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You are given a string ...

  2. How to diagnose vehicle fault code by BMW ICOM and ISTA-D software

    Today illustrate how to diagnose vehicle fault code by BMW diagnostic tool BMW ICOM and ISTA-D softw ...

  3. 前端框架VUE----webpack打包工具的使用

    在这里我仅仅的是对webpack做个讲解,webpack这个工具非常强大,解决了我们前端很繁琐的一些工具流程繁琐的事情.如果感兴趣的同学,还是看官网吧. 中文链接地址:https://www.webp ...

  4. makefile 双冒号规则

    双冒号规则就是使用“::”代替普通规则的“:”得到的规则.当同一个文件作为多个规则的目标时,双冒号规则的处理和普通规则的处理过程完全不同(双冒号规则允许在多个规则中为同一个目标指定不同的重建目标的命令 ...

  5. django rest framework跨表取值

  6. MySQL修改库名的方法

    先创建新的库,再用RENAME TABLE 语句移动旧库中的表到新库,最后删除旧库. (root@localhost) [(none)] create database mydb_2; Query O ...

  7. 判断PC或mobile设备

    js 限制: <script type="text/javascript"> function uaredirect(f){try{if(document.getEle ...

  8. 【题解】bzoj 4478 [Jsoi2013]侦探jyy

    原题传送门 弱智搜索题 我们就枚举每个点,先判断它是否必须发生,如果没有必须发生,开始搜索它的祖先,如果祖先中有必须发生的,那么它就必须发生,如果祖先中没有必须发生的,那么搜索所有入度为0的点(除了它 ...

  9. amqp 抓包

    1. wireshark 2. tcpick -yR -r  file.name

  10. ajax返回数据

    在使用远程js验证检测账户是否存在时,直在发请求后返回值无效,怎样把值返回回来呢重点注意两点 第一点:type不能省略,不能是异步,async: false 第二点:不能在直接请求成功后返回 var ...