vue.js精讲02

  • 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>

?

时间: 2024-12-23 15:37:47

vue.js精讲02的相关文章

vue.js精讲01

笔记及源码地址 : https://github.com/wll8/vue_note 01 2017-09-13 view一个 mvvm框架(库),和 ag 类似.比较小巧,容易上手. mvc: mvp mvvm mvx(mv*) vue 和 ag 的区别. 不用纠结什么好,项目适合什么就用什么. vue 简单 中文文档 指令: v-xxx 例子: html + json + vue实例 维护: 个人 适合: 移动 ag 上手难 英文文档 指令: ng-xxx 例子: 把所有属性和方法挂到 $s

vue.js初级教程--02.环境搭建

node.js 如何安装 官方网址  https://nodejs.org/en/ 如何验证node.js安装成功 node -v 能够解决什么问题 相较于其他服务器框架,它给系统性能能提升所带来的好处? node.js特性 javascript运行环境 依赖Chrome v8引擎进行代码解释 事件驱动 非阻塞I/O 轻量/可伸缩的 实时交互的应用面比较广(I/O密集型的服务器模型里面性能比较好) 单进程.单线程 解决的问题 并发连接数 案例 利用node.js http模块 我们监听端口888

vue.js 精学记录

v-bind:class使用的方式: 1.对象与法::class="{'active':isActive}" 此时当isActive为true时,会拥有active 2.数组语法:需要应用多个class时,可以使用数组语法. :class = " [activeCls,errorCls ] ", var app = new Vue({ el:"#app", data:{ activeCls:'active', errorCls:'error' }

第三章、vue基础精讲

3.9.Vue中的set方法 数组-----改变数组页面也跟着改变 第一.直接改变引用 第二.用数组的的实例方法 第三.用vue的set方法 改变对象,页面也跟着改变 第一.直接改变引用 第二.用vue的set方法(Vue.set || vm.$set) 原文地址:https://www.cnblogs.com/qdwz/p/11429102.html

Webpack + React全栈工程架构项目实战精讲

详情请交流  QQ  709639943 01.Webpack + React全栈工程架构项目实战精讲 02.跨平台混编框架 MUI 仿豆瓣电影 APP 03.Node.js入门到企业Web开发中的应用 04.Python3 全网最热的Python3入门+进阶 比自学更快上手实际开发 05.Java秒杀系统方案优化 高性能高并发实战 06.Java深入微服务原理改造房产销售平台 07.快速上手Linux 玩转典型应用 08.全面系统讲解CSS 工作应用+面试一步搞定 09.Java Spring

Vue.js 组件精讲

课程介绍:你会学到什么        了解 Vue.js 组件开发的精华        Vue.js 组件知识全覆盖        掌握多种 Vue.js 组件开发的模式        独立组件不依赖 Vuex 和 Bus 情况下,各种跨级通信手段(provide / inject.broadcast / dispatch.findComponents 系列)        7 个完整的 Vue.js 组件示例        如何做好一个开源项目        Vue.js 容易忽略的 API

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

VUE 源码分析 简介 Vue 是 MVVM 框架中的新贵,如果我没记错的话作者应该毕业不久,现在在google.vue 如作者自己所说,在api设计上受到了很多来自knockout.angularjs等大牌框架影响,但作者相信 vue 在性能.易用性方面是有优势.同时也自己做了和其它框架的性能对比,在这里.今天以版本 0.10.4 为准 入口 Vue 的入口也很直白: ? 1 var demo = new Vue({ el: '#demo', data: { message: 'Hello V

vue.js基础知识篇(2):指令详解

第三章:指令 1.语法 指令以v-打头,它的值限定为绑定表达式,它负责的是按照表达式的值应用某些行为到DOM上. 内部指令有v-show,v-else,v-model,v-repeat,v-for,v-text,v-el,v-html,v-on,v-bind,v-ref,v-pre,v-cloak,v-if. 2.内部指令 (1)控制元素的显示与否:v-if,v-show.v-else v-if是真实的条件渲染,根据表达式的true/false在DOM中生成或移除一个元素. 第一,这个指令是惰性

vue.js基础知识篇(1):简介、数据绑定、指令、计算属性、表单控件绑定和过滤器

目录第一章:vue.js是什么? 代码链接: http://pan.baidu.com/s/1qXCfzRI 密码: 5j79 第一章:vue.js是什么? 1.vue.js是MVVM框架 MVVM的代表框架是Angular.js,以及vue.js. MVVM的view和model是分离的,View的变化会自动更新到ViewModel上,ViewModel的变化会自动同步到View上显示.这种自动同步依赖于ViewModel的属性实现了Observer. 2.它与angular.js的区别 相同