指令和事件

指令是Vue模板中最常用的一項功能,前綴v-。指令的主要職責就是當其表達式的值改變時,相應的將某些行為應用到DOM上。

v-if

用來判斷元素是否插入,為true的時候元素會被插入,為false的時候元素擇會被移除。

示例代碼( isShow = true )
<template>
  <div id="app">
   {{date|formatDate}}
   <span v-if="isShow">{{msg}}</span>
  </div>
</template>

<script>
import Util from "./utils/util.js";
export default {
  name: "app",
  data() {
    return {
      date: new Date(),
      msg:"Hello world!",
      isShow:true
    };
  },

  filters: {
    formatDate: function(date) {
      return `${date.getFullYear()}-${Util.format(
        date.getMonth() + 1
      )}-${Util.format(date.getDate())} ${Util.format(
        date.getHours()
      )}:${Util.format(date.getMinutes())}:${Util.format(date.getSeconds())}`;
    }
  }
};
</script>
結(jié)果:( isShow = true )
image.png
示例代碼( isShow = false )
<template>
  <div id="app">
   {{date|formatDate}}
   <span v-if="isShow">{{msg}}</span>
  </div>
</template>

<script>
import Util from "./utils/util.js";
export default {
  name: "app",
  data() {
    return {
      date: new Date(),
      msg:"Hello world!",
      isShow:false
    };
  },

  filters: {
    formatDate: function(date) {
      return `${date.getFullYear()}-${Util.format(
        date.getMonth() + 1
      )}-${Util.format(date.getDate())} ${Util.format(
        date.getHours()
      )}:${Util.format(date.getMinutes())}:${Util.format(date.getSeconds())}`;
    }
  }
};
</script>
結(jié)果:( isShow = false )
image.png

v-bind

用來動態(tài)更新HTML元素上的屬性
縮寫

<a v-bind:href="url">百度一下</a>
<!-- 縮寫為 -->
<a :href="url">百度一下</a>
實例代碼
<template>
  <div id="app">
   {{date|formatDate}}
   <span v-if="isShow">{{msg}}</span>
   <p><a v-bind:href="url">百度一下</a></p>
  </div>
</template>

<script>
import Util from "./utils/util.js";
export default {
  name: "app",
  data() {
    return {
      date: new Date(),
      msg:"Hello world!",
      isShow:false,
      url:'https://www.baidu.com'
    };
  },

  filters: {
    formatDate: function(date) {
      return `${date.getFullYear()}-${Util.format(
        date.getMonth() + 1
      )}-${Util.format(date.getDate())} ${Util.format(
        date.getHours()
      )}:${Util.format(date.getMinutes())}:${Util.format(date.getSeconds())}`;
    }
  }
};
</script>

v-on

用來綁定事件監(jiān)聽器,這樣我們就可以做一些交互。
縮寫@

<button v-on:click="handleShow">{{btnTitle}}</button>
<!-- 縮寫為 -->
<button @click="handleShow">{{btnTitle}}</button>
示例代碼
template>
  <div id="app">
   {{date|formatDate}}
   <span v-if="isShow">{{msg}}</span>
   <p><a v-bind:href="url">百度一下</a></p>
   <button v-on:click="handleShow">{{btnTitle}}</button>
  </div>
</template>

<script>
import Util from "./utils/util.js";
export default {
  name: "app",
  data() {
    return {
      date: new Date(),
      msg: "Hello world!",
      isShow: false,
      url: "https://www.baidu.com",
      btnTitle: "顯示"
    };
  },
  methods: {
    handleShow: function() {
      this.isShow = !this.isShow;
      if (this.isShow) {
        this.btnTitle = "隱藏";
      } else {
        this.btnTitle = "顯示";
      }
    }
  },
  filters: {
    formatDate: function(date) {
      return `${date.getFullYear()}-${Util.format(
        date.getMonth() + 1
      )}-${Util.format(date.getDate())} ${Util.format(
        date.getHours()
      )}:${Util.format(date.getMinutes())}:${Util.format(date.getSeconds())}`;
    }
  }
};
</script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,662評論 19 139
  • There was once a blacksmith who had a little dog. While h...
    stevenbill閱讀 290評論 0 0
  • 公元1到4世紀,密特拉從東方滲透入羅馬文化。這個雕塑顯示了羅馬密特拉神穿著類似帕蒂亞人服裝,有力的征戰(zhàn)一頭用來祭祀的牛。
    虎斑貓_閱讀 812評論 0 1

友情鏈接更多精彩內(nèi)容