[关闭]
@GivenCui 2017-06-05T23:11:39.000000Z 字数 5108 阅读 763

Vue.js学习

Vue


相关链接和知识点关键词摘要

链接

知识点关键词摘要

  1. 双向绑定的实现原理
  2. virtual DOM的原理

2016智能社Vue视频学习记录

class1

基本结构

一片html代码配合上json,在new出来vue实例

  1. var vm = new Vue({
  2. el:'#box', // 选择器
  3. data:{ // 数据 (属性)
  4. arr:['apple','banana','orange','pear'],
  5. json:{a:'apple',b:'banana',c:'orange'}
  6. },
  7. methods : { // 方法
  8. show : function() { console.log(123) }
  9. }
  10. });

vue实例属性和方法

Vue 实例暴露了一些有用的实例属性与方法。这些属性与方法都有前缀 $

  1. var vm = new Vue({
  2. el: '#example',
  3. data: {
  4. a : 123,
  5. b : 'test'
  6. }
  7. })
  8. vm.$data === data // -> true
  9. vm.$el === document.getElementById('example') // -> true
  10. // $watch 是一个实例方法
  11. vm.$watch('a', function (newVal, oldVal) {
  12. // 这个回调将在 `vm.a` 改变后调用
  13. })

模板语法

  1. /* ============
  2. v-for
  3. ============*/
  4. <ul>
  5. <li v-for="value in arr">
  6. {{value}} {{$index}}
  7. </li>
  8. </ul>
  9. <ul>
  10. <li v-for="value in json">
  11. {{value}} {{$index}} {{$key}}
  12. </li>
  13. </ul>
  14. <ul>
  15. <li v-for="(k,v) in json">
  16. {{k}} {{v}} {{$index}} {{$key}}
  17. </li>
  18. </ul>

demo-简易留言板(todoList)

技术要点: Bootstrap
总结: Vue是以数据驱动view, 业务逻辑在于改变数据,几乎用不到dom操作

Bootstrap模态框
  1. <!-- data-toggle:弹出, data-target:弹出对象 -->
  2. <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer">删除</button>
  3. <!-- 模态框 弹出框 -->
  4. <!-- data-dismiss: 关闭自身 id对应data-target -->
  5. <div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
  6. <div class="modal-dialog">
  7. <div class="modal-content">
  8. <div class="modal-header">
  9. <button type="button" class="close" data-dismiss="modal">
  10. <span>&times;</span>
  11. </button>
  12. <h4 class="modal-title">确认删除么?</h4>
  13. </div>
  14. <div class="modal-body text-right">
  15. <button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
  16. <button data-dismiss="modal" class="btn btn-danger btn-sm">确认</button>
  17. </div>
  18. </div>
  19. </div>
  20. </div>
v-show的应用场景

v-if适合第一次渲染的情况, 或变动小的情况
v-show适合显示,隐藏;频繁切换的情况

  1. <tr v-show="myData.length!=0">
  2. <td colspan="4" class="text-right">
  3. <button class="btn btn-danger btn-sm">删除全部</button>
  4. </td>
  5. </tr>
  6. <tr v-show="myData.length==0">
  7. <td colspan="4" class="text-center text-muted">
  8. <p>暂无数据....</p>
  9. </td>
  10. </tr>

demo-百度下拉列表

技术要点: vue-resource.js (ajax的get,post,jsonp等)
总结: 客户端通过get,post,jsonp等方式与服务器数据交互(服务器进行数据库的CURD操作)

  1. // 精华:
  2. <li v-for="value in myData" :class="{gray:$index==now}">
  3. {{value}}
  4. </li>
  1. new Vue({
  2. el:'body',
  3. data:{
  4. myData:[],
  5. t1:'',
  6. now:-1
  7. },
  8. methods:{
  9. get:function(ev){
  10. // 排除上下键时出发多余的请求
  11. // 太经典了
  12. if(ev.keyCode==38 || ev.keyCode==40)return;
  13. // 回车选中后跳转
  14. if(ev.keyCode==13){
  15. window.open('https://www.baidu.com/s?wd='+this.t1); // 打开新窗口跳转
  16. // location.href = 'https://www.baidu.com/s?wd='+this.t1; // 原页面跳转
  17. this.t1='';
  18. }
  19. // jsonp请求部分
  20. this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{
  21. wd:this.t1
  22. },{
  23. jsonp:'cb'
  24. }).then(function(res){
  25. this.myData=res.data.s;
  26. },function(){
  27. });
  28. },
  29. changeDown:function(){
  30. this.now++;
  31. if(this.now==this.myData.length)this.now=-1; // 最后一个调到最上面
  32. this.t1=this.myData[this.now]; // this.myData[-1] = undefined
  33. },
  34. changeUp:function(){
  35. this.now--;
  36. if(this.now==-2)this.now=this.myData.length-1;
  37. this.t1=this.myData[this.now];
  38. }
  39. }
  40. });
  41. <!-- HTML -->
  42. <div id="box">
  43. <input type="text" v-model="t1" @keyup="get($event)" @keydown.down="changeDown()" @keydown.up.prevent="changeUp()"><!--阻止按上键输入框中的光标移动-->
  44. <ul>
  45. <li v-for="value in myData" :class="{gray:$index==now}">
  46. {{value}}
  47. </li>
  48. </ul>
  49. <p v-show="myData.length==0">暂无数据...</p>
  50. </div>
vue-resource的用法

$ajax和基本http概念
vue-resource全攻略
API

  1. /* Response的API */
  2. {
  3. // Constructor
  4. constructor(any: body, object: {string: url, object: headers, number: status, string: statusText})
  5. // Properties
  6. url (string)
  7. body (any)
  8. headers (Headers)
  9. ok (boolean)
  10. status (number)
  11. statusText (string)
  12. // Methods
  13. blob() (Promise)
  14. text() (Promise)
  15. json() (Promise)
  16. }
http和$http
  1. // 基于全局Vue对象使用http
  2. Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
  3. Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
  4. // 在一个Vue实例内使用$http
  5. this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
  6. this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
GET
  1. /* get:获取数据 */
  2. /* 通过get获取本地的a.txt中的内容 */
  3. new Vue({
  4. el:'body',
  5. data:{
  6. },
  7. methods:{
  8. get:function(){
  9. this.$http.get('a.txt').then(function(res){
  10. alert(res.data);
  11. },function(res){
  12. alert(res.status);
  13. });
  14. }
  15. }
  16. });
  1. /* get:向服务器发送数据 */
  2. /* get(url, option)中option为get所传参数 */
  3. new Vue({
  4. el:'body',
  5. data:{
  6. },
  7. methods:{
  8. get:function(){
  9. this.$http.get('get.php',{
  10. a:1,
  11. b:2
  12. }).then(function(res){
  13. alert(res.data); // 3 (a + b)
  14. },function(res){
  15. alert(res.status);
  16. });
  17. }
  18. }
  19. });
  20. /* get.php */
  21. <?php
  22. $a=$_GET['a'];
  23. $b=$_GET['b'];
  24. echo $a+$b;
  25. ?>
POST
  1. new Vue({
  2. el: 'body',
  3. data: {},
  4. methods: {
  5. get: function () {
  6. this.$http.post('post.php', {
  7. a: 1,
  8. b: 20
  9. }, {
  10. headers: {
  11. "X-Requested-With": "XMLHttpRequest"
  12. },
  13. emulateJSON: true // 启用该选项后,请求会以application/x-www-form-urlencoded作为MIME type
  14. }).then(function (res) {
  15. alert(res.data);
  16. }, function (res) {
  17. alert(res.status);
  18. });
  19. }
  20. }
  21. });
  22. /* post.php */
  23. <?php
  24. $a=$_POST['a'];
  25. $b=$_POST['b'];
  26. echo $a-$b;
  27. ?>
JSONP
  • JSONP只支持GET方法, CORS(跨域资源共享)支持GET、POST、PUT、DELETE,但不支持老旧浏览器
  • callback函数的函数名由服务端决定
  • CORS浏览器支持情况可在caniuse.com中查询
  • JSONP相当于GET方法, 所以不用写 emulateJSON: true
  1. /* 百度输入框输入a,获得的链接简化后得到 */
  2. /* 参数解析:
  3. wd=a --> "wd":"a"
  4. cb=jshow --> cb为回调函数名, jshow随便改对数据无影响
  5. */
  6. https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=jshow
  7. /* 返回的数据为
  8. jshow({q:"a",p:false,s:["爱奇艺","阿里巴巴批发网","airbnb","acfun","阿里云","阿里云邮箱","阿里妈妈","安居客","阿里巴巴","安全教育平台"]});
  9. */
  10. new Vue({
  11. el:'body',
  12. data:{
  13. },
  14. methods:{
  15. get:function(){
  16. this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{
  17. wd:'a'
  18. },{
  19. jsonp:'cb' // callback函数的函数名, 默认值为callback,默认值可以省略
  20. }).then(function(res){
  21. alert(res.data.s);
  22. },function(res){
  23. alert(res.status);
  24. });
  25. }
  26. }
  27. });

class2

lifecycle.png-114kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注