最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue中v-on的基礎(chǔ)用法、參數(shù)傳遞和修飾符的示例詳解

 更新時(shí)間:2022年08月19日 10:43:46   作者:小余努力搬磚  
使用v-on:click給button綁定監(jiān)聽事件以及回調(diào)函數(shù),@是v-on:的縮寫,也就是簡(jiǎn)寫也可以使用@click,這篇文章主要介紹了Vue中v-on的基礎(chǔ)用法、參數(shù)傳遞和修飾符,需要的朋友可以參考下

一、v-on的基本用法

使用v-on:click給button綁定監(jiān)聽事件以及回調(diào)函數(shù),@是v-on:的縮寫,也就是簡(jiǎn)寫也可以使用@click。方法一般是需要寫方法名加上(),在@click中可以省掉,如上述的<button @click="increment">加</button>。

以簡(jiǎn)單的計(jì)數(shù)器為例

 <body>
  <div id="app">
      <h2>{{count}}</h2>
      <!-- <button v-on:click="count++">加</button>
      <button v-on:click="count--">減</button> -->
      <button @click="increment">加</button>
      <button @click="decrement">減</button>
  </div>
  <script>
    const app = new Vue({
      el:"#app",
      data:{
        count:0
      },
      methods: {
        increment(){
          this.count++
        },
        decrement(){
          this.count--
        }
      }
    })
 
  </script>
</body>

二、v-on的參數(shù)傳遞

在methods中定義的方法,我們可以進(jìn)行調(diào)用,可以帶上(),也可以不帶()

如下的btnClick的調(diào)用中,加了()想要調(diào)用,里面必須要有值,如果不加小括號(hào),就只會(huì)調(diào)用事件對(duì)象。

btnClick3想要傳入event,需要在調(diào)用時(shí)寫$event,才能調(diào)用事件對(duì)象。

  <div id="app">
    <!-- 事件沒傳參 -->
    <button @click="btnClick">按鈕1</button>
    <button @click="btnClick()">按鈕2</button>
    <!-- 事件調(diào)用方法傳參,寫函數(shù)時(shí)候省略小括號(hào),但是函數(shù)本身需要傳遞一個(gè)參數(shù) -->
    <button @click="btnClick2(123)">按鈕3</button>
    <button @click="btnClick2()">按鈕4</button>
    <button @click="btnClick2">按鈕5</button>
    <!-- 事件調(diào)用時(shí)候需要傳入event還需要傳入其他參數(shù) -->
    <button @click="btnClick3($event,123)">按鈕6</button>
  </div>
  <script src="vue.js"></script>
  <script>
    const app = new Vue({
      el:"#app",
      methods:{
        btnClick(){
          console.log("點(diǎn)擊XXX");
        },
        btnClick2(value){
          console.log(value+"----------");
        },
        btnClick3(event,value){
          console.log(event+"----------"+value);
        }
      }
    })
  </script>

事件沒傳參,可以省略()事件調(diào)用方法傳參了,寫函數(shù)時(shí)候省略了小括號(hào),但是函數(shù)本身是需要傳遞一個(gè)參數(shù)的,這個(gè)參數(shù)就是原生事件event參數(shù)傳遞進(jìn)去如果同時(shí)需要傳入某個(gè)參數(shù),同時(shí)需要event是,可以通過$event傳入事件。

三、v-on的修飾詞

.stop的使用,btn的click事件不會(huì)傳播,不會(huì)冒泡到上層,調(diào)用event.stopPropagation()

.prevent調(diào)用event.preeventDefault阻止默認(rèn)行為。

.enter監(jiān)聽鍵盤事件;

once事件只觸發(fā)一次(常用);

capture使用事件的捕獲模式;

self只有event.target是當(dāng)前操作的元素時(shí)才觸發(fā)事件;

passive事件的默認(rèn)行為立即執(zhí)行,無需等待事件回調(diào)執(zhí)行完畢;

 
  <style>
	  .div {
	    height:80px;
	    background:#f00;
	  }
  </style>
</head>
<body>
  <div id="app">
    <!--1. .stop的使用,btn的click事件不會(huì)傳播,不會(huì)冒泡到上層,調(diào)用event.stopPropagation() -->
    <div @click="divClick">
        <button @click.stop="btnClick">按鈕1</button>
    </div>
    <!-- 2. .prevent 調(diào)用event.preeventDefault阻止默認(rèn)行為  -->
    <form action="www.baidu.com">
      <button type="submit" @click.prevent="submitClick">提交</button>
    </form>
    <!--3. 監(jiān)聽鍵盤的事件 -->
    <form @submit.prevent=''>
	   賬號(hào)<input type="text" name="user"/>
	   密碼<input type="text" name="password" @keyup.enter="submit"/>
	    <input type="submit"  value="登錄"/>
   </form>
  <!--4. 事件只觸發(fā)一次(常用) -->
	<button @click.once="showInfo">點(diǎn)我提示信息</button>	  
  <!--5. capture使用事件的捕獲模式 -->
	<div class="box1" @click.capture="show(111)">
		div1外面
	<div class="box2" @click="show(222)">
		div2里面
	</div>
	</div>
 <!-- 6.只有event.target是當(dāng)前操作的元素時(shí)才觸發(fā)事件 -->
	<div class="div" @click.self="showself">
	   <button @click="showself">點(diǎn)我</button>
	</div>
<!-- 7.passive事件的默認(rèn)行為立即執(zhí)行,無需等待事件回調(diào)執(zhí)行完畢 -->	  
  </div>
  <script src="vue.js"></script>
  <script>
    const app = new Vue({
      el:"#app",
      methods:{
        btnClick(){
          console.log("點(diǎn)擊button");
        },
        divClick(){
          console.log("點(diǎn)擊div");
        },
        submitClcik(){
          console.log("提交被阻止了")
        },
        submit(){
          console.log("keyup點(diǎn)擊")
        },
	showInfo(){
	  alert('web學(xué)習(xí)真有趣')
	},
	show(msg){
	  console.log(msg)
	},
	showself(e){
	  console.log(e.target);
	},  
      }
    })
  </script>
</body>

到此這篇關(guān)于Vue中v-on的基礎(chǔ)用法、參數(shù)傳遞和修飾符的文章就介紹到這了,更多相關(guān)Vue中v-on參數(shù)傳遞和修飾符內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

安丘市| 云龙县| 左贡县| 望江县| 临朐县| 习水县| 乡城县| 利辛县| 高尔夫| 六枝特区| 涞水县| 三河市| 济宁市| 辽源市| 洪泽县| 成都市| 甘孜| 庄河市| 祁门县| 新河县| 博野县| 花莲市| 柯坪县| 申扎县| 乳源| 乌鲁木齐市| 沭阳县| 彩票| 阳江市| 沙坪坝区| 顺昌县| 南江县| 清丰县| 察雅县| 民乐县| 宝鸡市| 永善县| 盖州市| 阳朔县| 基隆市| 康定县|