博主信息
博文 47
粉丝 0
评论 0
访问量 31335
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
Vue的组件
P粉036614676
原创
479人浏览过

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');
批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学