当前位置:   article > 正文

Vue this.$refs获取DOM元素_vue获取dom元素$resfs

vue获取dom元素$resfs
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <div id="app"></div>
  10. <script src="./node_modules/vue/dist/vue.js"></script>
  11. <script>
  12. Vue.component('Sub', {
  13. template: `
  14. <div class="sub">子组件</div>
  15. `
  16. });
  17. var App = {
  18. template: `
  19. <div>
  20. <input type="text" ref="input">
  21. <button ref="btn1">btn1</button>
  22. <button ref="btn2">btn2</button>
  23. <Sub ref="subref"/>
  24. </div>
  25. `,
  26. created() {
  27. console.log(this.$refs.input); // undefined
  28. },
  29. beforeMount() {
  30. console.log(this.$refs.input); // undefined
  31. },
  32. mounted() {
  33. console.log(this.$refs.input); // <input type="text">
  34. console.log(this.$refs.btn1); // <button>btn1</button>
  35. console.log(this.$refs.btn2); // <button>btn2</button>
  36. console.log(this.$refs.subref); // VueComponent Sub子组件
  37. }
  38. };
  39. new Vue({
  40. el: '#app',
  41. data() {
  42. return {
  43. }
  44. },
  45. components: {
  46. App
  47. },
  48. template: `<App />`
  49. });
  50. </script>
  51. </body>
  52. </html>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/133985
推荐阅读