博客列表 >实例演示组件注册的二种方式,以及组件之间的通信方式

实例演示组件注册的二种方式,以及组件之间的通信方式

P粉314265155
P粉314265155原创
2022年08月09日 07:43:42293浏览

组件

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- vue 指令 本质 是 自定义属性 -->
  13. <!-- <button-counter></button-counter> 必须这种格式 -->
  14. <button-counter></button-counter>
  15. <button-counter></button-counter>
  16. </div>
  17. <template id="counter" class="uo">
  18. <button @click="count++">点赞: {{count}}</button>
  19. </template>
  20. <hr>
  21. <script>
  22. const app = Vue.createApp();
  23. // 注册组件两种方式
  24. // 1、全局组件 注册在vue实例上
  25. app.component('button-counter', {
  26. // 可以简写 将 <button @click ="count++">点赞:{{count}}</button> 放到html 的body 实现代码复用
  27. // 不能用 class 只能用 id
  28. template: '#counter',
  29. // template: '.uo',
  30. data() {
  31. return {
  32. count:0,
  33. }
  34. },
  35. }).mount('.app');
  36. </script>
  37. </body>
  38. </html>

组件

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- vue 指令 本质 是 自定义属性 -->
  13. <!-- <button-counter></button-counter> 必须这种格式 -->
  14. <button-counter></button-counter>
  15. <button-counter></button-counter>
  16. </div>
  17. <template id="counter" class="uo">
  18. <button @click="count++">点赞: {{count}}</button>
  19. </template>
  20. <hr>
  21. <script>
  22. // 2、局部组件 注册在vue实例的选项中
  23. const app = Vue.createApp({
  24. components: {
  25. // 可以使用规范的驼峰命名法, 不过在html中要转为蛇形
  26. // button-counter (不能用) buttonCounter
  27. buttonCounter : {
  28. template: '#counter',
  29. data() {
  30. return {
  31. count: 0,
  32. };
  33. },
  34. },
  35. },
  36. });
  37. app.mount('.app');
  38. </script>
  39. </body>
  40. </html>

父向子组件传参

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- vue 指令 本质 是 自定义属性 -->
  13. <!-- <button-counter></button-counter> 必须这种格式 -->
  14. <!-- 使用自定义属性将父组 件参数传入到子组件中 -->
  15. <button-counter username = 'admin' email = '123@qq.com'></button-counter>
  16. </div>
  17. <template id="counter" class="uo">
  18. <p>用户:{{username}}</p>
  19. <p>邮箱:{{email}}</p>
  20. </template>
  21. <script>
  22. const app = Vue.createApp();
  23. app.component('button-counter', {
  24. template: '#counter',
  25. // props 属性 用数组来而棘手父组件传入的自定义属性作为载体的参数
  26. props:['username','email'],
  27. });
  28. app.mount('.app');
  29. </script>
  30. </body>
  31. </html>

子向父组件传参:监听了组件事件

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- vue 指令 本质 是 自定义属性 -->
  13. <!-- <button-counter></button-counter> 必须这种格式 -->
  14. <!-- 使用自定义属性将父组 件参数传入到子组件中 -->
  15. <button-counter @review-count="review"></button-counter>
  16. </div>
  17. <template id="counter" class="uo">
  18. <button @click="count++">点赞: {{count}}</button>
  19. <!-- 基础发布订阅
  20. $emit() 固定写法
  21. $emit(自定义事件的名称,像父组件传递的参数(可选))
  22. -->
  23. <button @click="$emit('reviewCount',this.count)">评价</button>
  24. </template>
  25. <script>
  26. const app = Vue.createApp({
  27. methods: {
  28. review (count){
  29. console.log(count);
  30. if(count >=10) alert('你们好')
  31. },
  32. },
  33. });
  34. app.component('button-counter', {
  35. template: '#counter',
  36. data() {
  37. return {
  38. count: 0,
  39. };
  40. },
  41. });
  42. app.mount('.app');
  43. </script>
  44. </body>
  45. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议