说明

image.png
image.png

image.png

原理

image.png

支持的表单元素

  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>Document</title>
  8. </head>
  9. <body>
  10. <div id="app"></div>
  11. <template id="my-app">
  12. <!-- 1.绑定textarea -->
  13. <label for="intro">
  14. 自我介绍
  15. <textarea name="intro" id="intro" cols="30" rows="10" v-model="intro"></textarea>
  16. </label>
  17. <h2>intro: {{intro}}</h2>
  18. <!-- 2.checkbox -->
  19. <!-- 2.1.单选框 -->
  20. <label for="agree">
  21. <input id="agree" type="checkbox" v-model="isAgree"> 同意协议
  22. </label>
  23. <h2>isAgree: {{isAgree}}</h2>
  24. <!-- 2.2.多选框 -->
  25. <span>你的爱好: </span>
  26. <label for="basketball">
  27. <input id="basketball" type="checkbox" v-model="hobbies" value="basketball"> 篮球
  28. </label>
  29. <label for="football">
  30. <input id="football" type="checkbox" v-model="hobbies" value="football"> 足球
  31. </label>
  32. <label for="tennis">
  33. <input id="tennis" type="checkbox" v-model="hobbies" value="tennis"> 网球
  34. </label>
  35. <h2>hobbies: {{hobbies}}</h2>
  36. <!-- 3.radio -->
  37. <span>你的爱好: </span>
  38. <label for="male">
  39. <input id="male" type="radio" v-model="gender" value="male">
  40. </label>
  41. <label for="female">
  42. <input id="female" type="radio" v-model="gender" value="female">
  43. </label>
  44. <h2>gender: {{gender}}</h2>
  45. <!-- 4.select -->
  46. <span>喜欢的水果: </span>
  47. <select v-model="fruit" multiple size="2">
  48. <option value="apple">苹果</option>
  49. <option value="orange">橘子</option>
  50. <option value="banana">香蕉</option>
  51. </select>
  52. <h2>fruit: {{fruit}}</h2>
  53. </template>
  54. <script src="https://unpkg.com/vue@next"></script>
  55. <script>
  56. const App = {
  57. template: '#my-app',
  58. data() {
  59. return {
  60. intro: "Hello World",
  61. isAgree: false,
  62. hobbies: ["basketball"],
  63. gender: "",
  64. fruit: "orange" // 多选就会变数组
  65. }
  66. },
  67. methods: {
  68. commitForm() {
  69. axios
  70. }
  71. }
  72. }
  73. Vue.createApp(App).mount('#app');
  74. </script>
  75. </body>
  76. </html>

image.pngimage.png

components 组件模版

值绑定

image.png
对于单选按钮,复选框及选择框的选项,v-model 绑定的值通常是固定的字符串 或 布尔值,但是v-model可以更改这些固定的值。

比如单选框选中就是true,没选中就是false。现在可以通过绑定一个值,改变这个默认,选中就是A,没选中就是B

  1. <template>
  2. <div>
  3. <div v-for="(item, index) of options">
  4. <input :id="options[index].text" type="radio" v-model="inputed" v-bind:value="options[index].value" />
  5. <label :for="options[index].text">{{ options[index].text }}</label>
  6. </div>
  7. <br />
  8. <span>动态选项: {{ inputed }}</span>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. inputed: '',
  16. options: [
  17. { text: 'One', value: 'A' },
  18. { text: 'Two', value: 'B' },
  19. ]
  20. };
  21. }
  22. };
  23. </script>

修饰符

.lazy 不实时更新

image.png

.number 转换成数字

image.png

.trim 过滤首尾空白字符

image.png

======================

源码实现

image.png