Blogger Information
Blog 94
fans 0
comment 0
visits 92630
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【Vue】数据双向绑定、事件绑定
可乐随笔
Original
1361 people have browsed it

Vue 数据双向绑定、事件绑定

1. 数据双向绑定

总结:

  1. v-on:绑定行内事件,用在“事件属性上”;

2.$event:vue中的事件对象,代替event
事件主体:$event.target:<input>;

3.v-on 用 @ 代替

4.v-model:双向数据绑定,代替 @input="value=$event.targe.value"

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>数据的双向绑定</title>
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <p>
  12. <!-- ES6 原生数据双向绑定 -->
  13. <span>ES6:</span>
  14. <input type="text" name="" oninput="this.nextElementSibling.textContent = this.value">
  15. <span></span>
  16. </p>
  17. <hr>
  18. <!-- Vue 数据双向绑定 -->
  19. <!-- v-bind:绑定预定义属性 -->
  20. <div id="app">
  21. <span>Vue:</span>
  22. <!-- v-on:绑定行内事件,用在“事件属性上” -->
  23. <!-- $event:vue中的事件对象,代替event -->
  24. <!-- 事件主体:$event.target:<input> -->
  25. <!-- v-on 用 @ 代替 -->
  26. <!-- <input type="text" v-on:input="value=$event.target.value" :value="value"> -->
  27. <input type="text" @:input="value=$event.target.value" :value="value">
  28. <span>{{value}}</span>
  29. <hr>
  30. <!-- v-model:双向数据绑定,代替 @input="value=$event.targe.value" -->
  31. <span>Vue:</span>
  32. <input type="text" v-model="value" :value="value">
  33. <span>{{value}}</span>
  34. </div>
  35. <script>
  36. const app = Vue.createApp({
  37. data() {
  38. return {
  39. value: 'php1.cn',
  40. };
  41. },
  42. });
  43. const vm = app.mount('#app');
  44. </script>
  45. </body>
  46. </html>

2. 事件绑定

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>事件绑定</title>
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <!-- 函数方法实现:点击链接,输出链接 -->
  12. <p>
  13. 事件属性:
  14. <a href="https://www.php.cn" onclick="showUrl(this,event)">show Url</a>
  15. <span class="url"></span>
  16. </p>
  17. <script>
  18. // function showUrl(ev){
  19. // //禁用默认行为
  20. // ev.preventDefault();
  21. // // ev.target.href = https://www.php.cn
  22. // ev.target.nextElementSibling.textContent = ev.target.href;
  23. // }
  24. // 代码简化
  25. // function showUrl(ele, ev) {
  26. // //禁用默认行为
  27. // ev.preventDefault();
  28. // // ev.target.href = https://www.php.cn
  29. // ele.nextElementSibling.textContent = ele.href;
  30. // }
  31. </script>
  32. <hr>
  33. <!-- Vue 实现方法 -->
  34. <div class="app">
  35. <!-- @click.prevent:事件修饰符,禁用默认点击行为 -->
  36. <!-- stop:防止冒泡 -->
  37. <a href="https://www.php.cn" @click.prevent="showUrl($event)">show Url</a>
  38. <span class="url">{{url}}</span>
  39. </div>
  40. <script>
  41. Vue.createApp({
  42. //数据
  43. data() {
  44. return {
  45. url: null,
  46. };
  47. },
  48. //方法
  49. methods: {
  50. showUrl(ev){
  51. // ev.preventDefault();//事件禁用放到事件触发(@click后面)
  52. //数据注入:所有Data中声明的变量,都是实例的属性
  53. this.url = ev.currentTarget.href;
  54. },
  55. },
  56. }).mount('.app');
  57. </script>
  58. </body>
  59. </html>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post