• Vue入门常用指令详解


    Vue入门

    Vue是一个MVVM(Model / View / ViewModel)的前端框架,相对于Angular来说简单、易学上手快,近两年也也别流行,发展速度较快,已经超越Angular了。比较适用于移动端,轻量级的框架,文件小,运行速度快。最近,闲来无事,所以学习一下Vue这个流行的框架,以备后用。

    一、指令

    1. v-model 多用于表单元素实现双向数据绑定(同angular中的ng-model
    2. v-for 格式: v-for="字段名 in(of) 数组json"  循环数组或json(angular中的ng-repeat),需要注意从vue2开始取消了$index
    3. v-show 显示内容 (同angular中的ng-show
    4. v-hide  隐藏内容(同angular中的ng-hide
    5. v-if    显示与隐藏  (dom元素的删除添加 同angular中的ng-if 默认值为false
    6. v-else-if  必须和v-if连用
    7. v-else  必须和v-if连用  不能单独使用  否则报错   模板编译错误
    8. v-bind  动态绑定  作用: 及时对页面的数据进行更改
    9. v-on:click 给标签绑定函数,可以缩写为@,例如绑定一个点击函数  函数必须写在methods里面
    10. v-text  解析文本
    11. v-html   解析html标签
    12. v-bind:class   三种绑定方法  1、对象型  '{red:isred}'  2、三元型   'isred?"red":"blue"'   3、数组型  '[{red:"isred"},{blue:"isblue"}]'
    13. v-once  进入页面时  只渲染一次 不在进行渲染
    14. v-cloak  防止闪烁
    15. v-pre  把标签内部的元素原位输出

    二、基本组件属性

    复制代码
    1 new Vue({
    2   el,         // 要绑定的 DOM element 3 template, // 要解析的模板,可以是 #id, HTML 或某個 DOM element 4 data, // 要绑定的数据 5 computed, // 依赖于别的数据计算出来的数据, name = firstName + lastName 6 watch, // 监听方法, 监听到某一数据变化时, 需要做的对应操作 7 methods, // 定义可以在元件或模板內使用的方法 8 })
    复制代码

    三、基础使用

    1.html

    1 <div id="app"> 2 <p>{{msg}}</p> 3 </div>

    2.js

    复制代码
     1 var app=new Vue({
     2         el:'#app',//标签的类名、id,用于获取元素  3 //以键值对的形式存放用到的数据成员  4  data:{  5 msg:'显示的内容'  6  },  7 //包含要用到的函数方法  8  methods:{  9  } 10 });
    复制代码

    这样js中msg的内容就会在p标签内显示出来。

    四、实例

    利用bootstrap+vue实现简易留言板的功能,可以增加、删除,弹出模态框

    复制代码
      1 <!DOCTYPE html>
      2 <html lang="en">  3 <head>  4 <meta charset="UTF-8">  5 <title>简易留言板</title>  6 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  7 <meta name="apple-mobile-web-app-capable" content="yes">  8 <meta name="apple-mobile-web-app-status-bar-style" content="black">  9 <style>  10  11 </style>  12 <link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.min.css">  13 <script src="../../node_modules/jquery/dist/jquery.min.js"></script>  14 <script src="../../node_modules/bootstrap/dist/js/bootstrap.min.js"></script>  15  16 <script src="../../node_modules/vue/dist/vue.js"></script>  17 <script>  18  window.onload=function(){  19 new Vue({  20  el:'#box',  21  data:{  22  myData:[],  23  username:'',  24  age:'',  25  nowIndex:-100  26  },  27  methods:{  28  add:function(){  29 this.myData.push({  30  name:this.username,  31  age:this.age  32  });  33  34 this.username='';  35 this.age='';  36  },  37  deleteMsg:function(n){  38 if(n==-2){  39 this.myData=[];  40  }else{  41 this.myData.splice(n,1);  42  }  43  }  44  }  45  });  46  };  47 </script>  48 </head>  49 <body>  50 <div class="container" id="box">  51 <form role="form">  52 <div class="form-group">  53 <label for="username">用户名:</label>  54 <
  • 相关阅读:
    Git 使用规范流程
    关于Python的super用法研究
    python中try except处理程序异常的三种常用方法
    break 和 continue 语句, 以及循环中的 else 子句
    杂记(python)
    Request和Response
    MVC项目开发步骤
    Web中单元测试步骤
    JSP中的细节
    WEB中地址的写法
  • 原文地址:https://www.cnblogs.com/lxwphp/p/15453331.html
Copyright © 2020-2023  润新知