1.自定义指令.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. <!-- 引入vue -->
  9. <script type="text/javascript" src="../js/vue.js"></script>
  10. </head>
  11. <body>
  12. <!--
  13. 需求1:定义一个v-big指令,和v-text功能类似,但会把绑定的数值放大10倍。
  14. 需求2:定义一个v-fbind指令,和v-bind功能类似,但可以让其所绑定的input元素默认获取焦点。
  15. 自定义指令总结:
  16. 一、定义语法:
  17. 1、局部指令
  18. new Vue({
  19. directives:{指令名:配置对象}
  20. })
  21. new Vue({
  22. directives{指令名:回调函数}
  23. })
  24. 2、全局指令
  25. Vue.directive(指令名:配置对象)或Vue.directive(指令名:回调函数)
  26. 二、配置对象中常用的3个回调:
  27. 1、bind:指令与元素成功绑定时调用。
  28. 2、inserted:指令坐在元素被插入页面时调用
  29. 3、update:指令所在模板结构被重新解析时调用
  30. 三、备注
  31. 1、指令定义时不加v-,但使用时要加v-
  32. 2、指令名如果是多个单词,要使用kebab-case命名方式(即user-name),
  33. 不要用camelClass命名(即userName)
  34. -->
  35. <!-- 准备一个容器 -->
  36. <div id="root">
  37. <h2>{{name}}</h2>
  38. <h2>当前的n值是:<span v-text="n"></span></h2>
  39. <!-- <h2>放大10倍后的n值是:<span v-big-number="n"></span></h2> -->
  40. <h2>放大10倍后的n值是:<span v-big="n"></span></h2>
  41. <button @click="n++">点我n+1</button>
  42. <hr />
  43. <input type="text" v-fbind:value="n">
  44. </div>
  45. </body>
  46. <script>
  47. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示
  48. // 定义全局指令
  49. // Vue.directive('fbind', {
  50. // // 指令与元素成功绑定时(一开始)调用
  51. // bind(element, binding) {
  52. // element.value = binding.value
  53. // },
  54. // // 指令所在元素被插入页面时调用
  55. // inserted(element, binding) {
  56. // element.focus()
  57. // },
  58. // // 指定所在的模板被重新解析时调用
  59. // update(element, binding) {
  60. // element.value = binding.value
  61. // element.focus()
  62. // }
  63. // })
  64. // Vue.directive('big',function(element, binding){
  65. // element.innerText = binding.value * 10
  66. // })
  67. new Vue({
  68. el: '#root',
  69. data: {
  70. name: '尚硅谷',
  71. n: 1
  72. },
  73. directives: {
  74. // 写起来麻烦,但是能处理很多细节上的问题
  75. // big:{
  76. // k:v,
  77. // k:v,
  78. // k:v
  79. // }
  80. // 当有多个单词时,在结构中使用“-”进行连接,在定义函数时,需要添加单引号进行包裹
  81. // 'big-number'(element,binding){
  82. // element.innerText = binding.value*10
  83. // },
  84. big(element, binding) {
  85. console.log('big', this) //此处的this是Window
  86. // big函数何时会被调用?
  87. // 1、指令与元素成功绑定时(一开始)。2、指令所在的模板被重新解析时。
  88. // innerText:获取元素里的文本内容
  89. element.innerText = binding.value * 10
  90. // console.log(element,binding.value)
  91. // 验证a是否是DOM
  92. // console.dir(a)
  93. // console.log(a instanceof HTMLElement)
  94. },
  95. fbind: {
  96. // 常用函数有三个
  97. // 指令与元素成功绑定时(一开始)调用
  98. bind(element, binding) {
  99. console.log('fbind-bind', this) //此处的this是Window
  100. // console.log(element,binding)
  101. element.value = binding.value
  102. },
  103. // 指令所在元素被插入页面时调用
  104. inserted(element, binding) {
  105. console.log('fbind-inserted', this) //此处的this是Window
  106. // console.log(element,binding)
  107. element.focus()
  108. },
  109. // 指定所在的模板被重新解析时调用
  110. update(element, binding) {
  111. console.log('fbind-update', this) //此处的this是Window
  112. // console.log(element,binding)
  113. element.value = binding.value
  114. element.focus()
  115. }
  116. }
  117. }
  118. })
  119. </script>
  120. </html>