Vue常用指令詳解分析

Vue入門

Vue是一個MVVM(Model / View / ViewModel)的前端框架,相對於Angular來說簡單、易學上手快,近兩年也也別流行,發展速度較快,已經超越Angular了。比較適用於移動端,輕量級的框架,文件小,運行速度快。最近,閒來無事,所以學習一下Vue這個流行的框架,以備後用。

一、指令

  • v-model 多用於表單元素實現雙向數據綁定(同angular中的ng-model)
  • v-for 格式: v-for="字段名 in(of) 數組json" 循環數組或json(同angular中的ng-repeat),需要注意從vue2開始取消了$index
  • v-show 顯示內容 (同angular中的ng-show)
  • v-hide 隱藏內容(同angular中的ng-hide)
  • v-if 顯示與隱藏 (dom元素的刪除添加 同angular中的ng-if 默認值爲false)
  • v-else-if 必須和v-if連用
  • v-else 必須和v-if連用 不能單獨使用 否則報錯 模板編譯錯誤
  • v-bind 動態綁定 作用: 及時對頁面的數據進行更改
  • v-on:click 給標籤綁定函數,可以縮寫爲@,例如綁定一個點擊函數 函數必須寫在methods裏面
  • v-text 解析文本
  • v-html 解析html標籤
  • v-bind:class 三種綁定方法 1、對象型 '{red:isred}' 2、三元型 'isred?"red":"blue"' 3、數組型 '[{red:"isred"},{blue:"isblue"}]'
  • v-once 進入頁面時 只渲染一次 不在進行渲染
  • v-cloak 防止閃爍
  • v-pre 把標籤內部的元素原位輸出

前端全棧學習交流圈:866109386,面向1-3經驗年前端開發人員,幫助突破技術瓶頸,提升思維能力,羣內有大量PDF可供自取,更有乾貨實戰項目視頻進羣免費領取。

二、基本組件屬性

new Vue({
 el,     // 要綁定的 DOM element
 template,  // 要解析的模板,可以是 #id, HTML 或某個 DOM element
 data,    // 要綁定的數據
 computed,  // 依賴於別的數據計算出來的數據, name = firstName + lastName
 watch,   // 監聽方法, 監聽到某一數據變化時, 需要做的對應操作
 methods,  // 定義可以在元件或模板內使用的方法
})

三、基礎使用
1.html

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

2.js

var app=new Vue({
    el:'#app',//標籤的類名、id,用於獲取元素
    //以鍵值對的形式存放用到的數據成員
    data:{
      msg:'顯示的內容'   
    },
    //包含要用到的函數方法
    methods:{      
    }
  });

前端全棧學習交流圈:866109386,面向1-3經驗年前端開發人員,幫助突破技術瓶頸,提升思維能力,羣內有大量PDF可供自取,更有乾貨實戰項目視頻進羣免費領取。

這樣js中msg的內容就會在p標籤內顯示出來。

四、實例

利用bootstrap+vue實現簡易留言板的功能,可以增加、刪除,彈出模態框

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>簡易留言板</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <style>
 
  </style>
  <link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.min.css" rel="external nofollow" >
  <script src="../../node_modules/jquery/dist/jquery.min.js"></script>
  <script src="../../node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
 
  <script src="../../node_modules/vue/dist/vue.js"></script>
  <script>
   window.onload=function(){
     new Vue({
      el:'#box',
      data:{
        myData:[],
        username:'',
        age:'',
        nowIndex:-100
      },
      methods:{
        add:function(){
         this.myData.push({
           name:this.username,
           age:this.age
         });
 
         this.username='';
         this.age='';
        },
        deleteMsg:function(n){
         if(n==-2){
           this.myData=[];
         }else{
           this.myData.splice(n,1);
         }
        }
      }
     });
   };
  </script>
</head>
<body>
<div class="container" id="box">
  <form role="form">
   <div class="form-group">
     <label for="username">用戶名:</label>
     <input type="text" id="username" class="form-control" placeholder="輸入用戶名" v-model="username">
   </div>
   <div class="form-group">
     <label for="age">年 齡:</label>
     <input type="text" id="age" class="form-control" placeholder="輸入年齡" v-model="age">
   </div>
   <div class="form-group">
     <input type="button" value="添加" class="btn btn-primary" v-on:click="add()">
     <input type="reset" value="重置" class="btn btn-danger">
   </div>
  </form>
  <hr>
  <table class="table table-bordered table-hover">
   <h3 class="h3 text-info text-center">用戶信息表</h3>
   <tr class="text-danger">
     <th class="text-center">序號</th>
     <th class="text-center">名字</th>
     <th class="text-center">年齡</th>
     <th class="text-center">操作</th>
   </tr>
   <tr class="text-center" v-for="(item,index) in myData">
     <td>{{index+1}}</td>
     <td>{{item.name}}</td>
     <td>{{item.age}}</td>
     <td>
      <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer" v-on:click="nowIndex=$index">刪除</button>
     </td>
   </tr>
   <tr v-show="myData.length!=0">
     <td colspan="4" class="text-right">
      <button class="btn btn-danger btn-sm" v-on:click="nowIndex=-2" data-toggle="modal" data-target="#layer" >刪除全部</button>
     </td>
   </tr>
   <tr v-show="myData.length==0">
     <td colspan="4" class="text-center text-muted">
      <p>暫無數據....</p>
     </td>
   </tr>
  </table>
 
  <!--模態框 彈出框-->
  <div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
   <div class="modal-dialog">
     <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">確認刪除麼?</h4>
        <button type="button" class="close" data-dismiss="modal">
         <span>×</span>
        </button>
 
      </div>
      <div class="modal-body text-right">
        <button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
        <button data-dismiss="modal" class="btn btn-danger btn-sm" v-on:click="deleteMsg(nowIndex)">確認</button>
      </div>
     </div>
   </div>
  </div>
</div>
</body>
</html>

運行效果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章