Blogger Information
Blog 47
fans 0
comment 0
visits 21016
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue的组件
P粉036614676
Original
329 people have browsed it

1.Vue组件本质

自定义属性,自定义标签

  1. <!--
  2. 1. 全局组件: 注册在vue实例上
  3. 2. 局部组件: 注册vue实例的选项中
  4. -->
  5. <!-- <div class="app">
  6. <button-counter></button-counter>
  7. </div>
  8. <script>
  9. const app =Vue.createApp();
  10. app.component('button-counter',{
  11. template:'<button>asdf</button>'
  12. });
  13. app.mount('.app');
  14. </script> -->
  15. <div class="app">
  16. <button-counter></button-counter>
  17. </div>
  18. <template id="counter">
  19. <button @click="count++">点赞: {{count}}</button>
  20. </template>
  21. <script>
  22. const app =Vue.createApp();
  23. app.component('button-counter',{
  24. template:'#counter',
  25. data(){
  26. return{
  27. count:0
  28. }
  29. }
  30. });
  31. app.mount('.app');
  32. </script>

2.向子组件传参

  1. <div id="app">
  2. <!-- 使用自定义属性将父组件参数传入子组件中 -->
  3. <button-counter username="admin" email="498446472@qq.com"></button-counter>
  4. </div>
  5. <template id="counter">
  6. <p>用户名: {{username}}</p>
  7. <p>邮箱: {{email}}</p>
  8. </template>
  9. <script>
  10. const app = Vue.createApp();
  11. app.component('button-counter', {
  12. // props: 用数组接收父组件传入的自定义属性做为载体的参数
  13. props: ['username', 'email'],
  14. template: '#counter',
  15. });
  16. app.mount('#app');
  17. </script>

3.向父组件传参

  1. div id="app">
  2. <button-counter @review-count="review"></button-counter>
  3. </div>
  4. <template id="counter">
  5. <button @click="count++">点赞: {{count}}</button>
  6. <!-- 发布订阅 -->
  7. <!-- $emit(自定义事件名称, 向父组件传递的参数(可选)) -->
  8. <!-- $emit('reviewCount', this.count) -->
  9. <button @click="$emit('reviewCount', this.count)">评价</button>
  10. </template>
  11. <script>
  12. const app = Vue.createApp({
  13. methods: {
  14. review(count) {
  15. console.log(count);
  16. if (count >= 10) alert('大家吃了吗?');
  17. },
  18. },
  19. });
  20. app.component('button-counter', {
  21. template: '#counter',
  22. data() {
  23. return {
  24. count: 0,
  25. };
  26. },
  27. });
  28. app.mount('#app');
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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