用到的代碼: https://github.com/liyang1234...
vue.js API傳送門: https://cn.vuejs.org/v2/api/html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>v-on指令</title> </head> <body> <div id="app"> <button v-on:click="clickhandler">btn</button> </div> <script src="vue.min.js"></script> <script> let vm = new Vue({ el: '#app', data: {}, methods: { clickhandler(){ console.log(Math.random()); } } }); </script> </body> </html>
點擊按鈕產生了隨機數,v-on:能夠用@代替。vue
<div id="app"> <button v-on:click="clickhandler">btn</button> <h1>{{num}}</h1> </div> <script src="vue.min.js"></script> <script> let vm = new Vue({ el: '#app', data: { num: 0 }, methods: { clickhandler(){ this. num++; } } }); </script>
點擊按鈕數字增長git
<div id="app"> <button v-on:click="clickhandler(2)">btn</button> <!-- 至關於函數調用 2是實參 --> <h1 v-text="num"></h1> </div> <script src="vue.min.js"></script> <script> let vm = new Vue({ el: '#app', data: { num: 0 }, methods: {//至關於函數聲明 clickhandler(n){ this. num += n; } } }); </script>
這樣就是每點擊一次增長2github
<div id="app"> <button v-on:click="clickhandler(2,$event)">btn</button> <!-- 至關於函數調用 2是實參 --> <h1 v-text="num"></h1> </div> <script src="vue.min.js"></script> <script> let vm = new Vue({ el: '#app', data: { num: 0 }, methods: {//至關於函數聲明 clickhandler(n,e){ this. num += n; console.log(e); } } }); </script>
<div id="app"> <button v-on:click="clickhandler(2,$event)">btn</button> <!-- 至關於函數調用 2是實參 --> <h1 v-text="num"></h1> <button v-on="{mousedown: doThis, mouseup: doThat}">btn2</button> </div> <script src="vue.min.js"></script> <script> let vm = new Vue({ el: '#app', data: { num: 0 }, methods: {//至關於函數聲明 clickhandler(n,e){ this. num += n; console.log(e); }, doThis(e){ console.log('doThis'); }, doThat(e){ console.log('doThat'); } } }); </script>
按下鼠標輸出doThis 放開鼠標輸出doThat
api
<style> #div1{ width: 400px; height: 400px; background: red; } #div2{ width: 200px; height: 200px; background: green; } </style>
</head>
<body>瀏覽器
<div id="app"> <button v-on:click="clickhandler(2,$event)">btn</button> <!-- 至關於函數調用 2是實參 --> <h1 v-text="num"></h1> <button v-on="{mousedown: doThis, mouseup: doThat}">btn2</button> <div id="div1" @click="clickDiv1"> <div id="div2" @click="clickDiv2"></div> </div> </div> <script src="vue.min.js"></script> <script> let vm = new Vue({ el: '#app', data: { num: 0 }, methods: {//至關於函數聲明 clickhandler(n,e){ this. num += n; console.log(e); }, doThis(e){ console.log('doThis'); }, doThat(e){ console.log('doThat'); }, clickDiv1(){ console.log('div1'); }, clickDiv2(){ console.log('div2'); } } }); </script>
點擊div2 再點擊div1app
先捕獲後冒泡,冒泡從裏往外,先輸出div2,再輸出div1。
<div id="div1" @click="clickDiv1"> <div id="div2" @click="clickDiv2($event)"></div> </div> clickDiv1(){ console.log('div1'); },
clickDiv2(e){dom
console.log('div2'); e.stopPropagation(); }
點擊div2,只輸出div2
函數
<div id="div1" @click="clickDiv1"> <div id="div2" @click.stop="clickDiv2($event)"></div> </div> clickDiv1(){ console.log('div1'); },
clickDiv2(e){ui
console.log('div2'); }
一樣能夠阻止冒泡
baidu
點擊超連接會默認跳轉到百度網頁,
<a href="http://www.baidu.com" @click="baidu($event)">baidu</a> baidu(e){ e.preventDefault(); }
點擊超連接以後不會跳轉了
vue提供的修飾符 .prevent
<a href="http://www.baidu.com" @click.prevent="baidu($event)">baidu</a>
一樣能阻止默認行爲
<a href="http://www.baidu.com" @click.stop.prevent="baidu($event)">baidu</a>
<input type="text" @keyup="keyuphandler($event)"> keyuphandler(e){ if(e.keyCode == 13){//13是回車 console.log('hahahahh'); } } 當在輸入框中按回車鍵時,輸出一串 。。。
keyCode總結:https://www.cnblogs.com/daysm...
<button v-on:click.once="clickhandler(2,$event)">btn</button>
按鈕只能點擊一次
<div id="app"> 用戶名: <input type="text" v-model="username"> <h4>用戶名是:{{username}}</h4> </div> <script src=vue.min.js></script> <script> new Vue({ el: '#app', data: { username: '' } }); </script>
input框裏輸入什麼就顯示什麼
原理:<input>至關於View層,經過v-model綁定上data裏面的username,將數據自動同步到Model層,雙花括號將二者聯繫起來,Model層將數據同步到「用戶名是:」的View層,這就是雙向數據綁定。
v-model自動將radio設成單選
性別: <input type="radio">男 <input type="radio">女
如今是單選框能夠多選
性別: <input type="radio" name="sex">男 <input type="radio" name="sex">女
添加name屬性能夠實現單選
如今用v-model試一下
性別: <input type="radio" value="男" v-model="sex">男 <input type="radio" value="女" v-model="sex">女 <h4>用戶的性別是:{{sex}}</h4>
後臺接收數據接的是value
愛好:<input type="checkbox" value="籃球" v-model="hobbys">籃球 <input type="checkbox" value="足球" v-model="hobbys">足球 <input type="checkbox" value="排球" v-model="hobbys">排球 <h4>用戶的愛好是:{{hobbys}}</h4> data: { hobbys:[] }
公司:<select name="" id="" v-model="company"> <option value="百度">百度</option> <option value="百度">阿里巴巴</option> <option value="百度">騰訊</option> </select> <h4>用戶的公司是:{{company}}</h4> data:{ company:' ' }
<textarea value="自我介紹" cols="30" rows="10" id="" v-model="description"></textarea> <h4>個人描述是:{{description}}</h4> data: { description:'' }
.lazy 當鼠標挪出input框的時候纔回顯
用戶名: <input type="text" v-model="username"> <h4>用戶名是:{{username}}</h4> 用戶名lazy: <input type="text" v-model.lazy="username"> <h4>用戶名是:{{username}}</h4>
.number
年齡:<input type="text" v-model="age"> <h4>用戶的年齡是:{{age}}</h4>
獲得的數據類型是 string
年齡:<input type="text" v-model.number="age"> <h4>用戶的年齡是:{{age}}</h4>
獲得的數據類型是 number
將type改爲number,輸入框能夠有增長減小功能
年齡:<input type="number" v-model.number="age"> <h4>用戶的年齡是:{{age}}</h4>
.trim去掉首尾空格
用戶名trim: <input type="text" v-model.trim="username"> <h4>用戶名是:{{username}}</h4>
<body> <div id="app"> <img src="imgSrc" alt=""> </div> <script src="vue.min.js"></script> <script> new Vue({ el: '#app', data: { imgSrc: 'https://cn.vuejs.org/images/logo.png' } });
想把ImgSrc的地址做爲img標籤中的圖片,可是會報錯:
<img v-bind:src="imgSrc" alt="">OK了
v-bind的簡寫形式是冒號:
.aa{ width: 100px; height: 100px; background: #F00; } <div class="aa"></div>
.aa{ width: 100px; height: 100px; background: #F00; }
v-bind的方式
.aa{ width: 100px; height: 100px; background: #F00; } <div :class="className"></div> data:{ className:'aa' }
.aa{ width: 100px; height: 100px; background: #F00; } <div :class="className"></div> <div :class="{aa:isAA}"></div> data:{ isAA: true }
意思是樣式顯示取決於isAA是否爲true
<div :style="styleObj"></div> styleObj: { width: '100px', height: '100px', background: '#0f0' }
<body> <div id="app"> <h1>{{msg}}</h1> </div> <script src="vue.min.js"></script> <script> new Vue({ el: '#app', data: { msg: 'Hello' } }); </script>
<h1 v-pre>{{msg}}</h1>