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

Vue组件注册的二种方式,以及组件之间的通信方式

秋闲独醉
秋闲独醉原创
2022年08月08日 15:23:39286浏览

全局组件和局部组件注册

  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. <script src="https://unpkg.com/vue@next"></script>
  8. <title>Vue组件</title>
  9. </head>
  10. <body>
  11. <!-- 全局组件 -->
  12. <div id="app">
  13. <my-button></my-button>
  14. </div>
  15. <!-- 在外部给,template -->
  16. <!-- <template id="content">
  17. <button @click="++count">点攒{{count}}</button>
  18. </template> -->
  19. <script>
  20. const app = Vue.createApp({
  21. // 局部组件
  22. components: {
  23. //局部组件命名小驼峰,不过在html中要转为蛇形
  24. myButton: {
  25. template: `<button v-on:click="++count">点攒{{count}}</button>`,
  26. data() {
  27. return {
  28. count: 0,
  29. };
  30. },
  31. },
  32. },
  33. });
  34. // //全局组件
  35. // app.component("my-button", {
  36. // data() {
  37. // return {
  38. // count: 0,
  39. // };
  40. // },
  41. // //在内部给
  42. // // template: `<button v-on:click="++count">点攒{{count}}</button>`,
  43. // //在外部给
  44. // template: "#content",
  45. // });
  46. // //挂载Vue
  47. app.mount("#app");
  48. </script>
  49. </body>
  50. </html>

组件父参数传递到子组件

  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. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div id="app">
  12. <!-- //父组件 -->
  13. <!-- 使用自定义属性将父组件参数传入子组件中 -->
  14. <my-button v-bind:username="name" v-bind:pwd="pwd"></my-button>
  15. </div>
  16. <template id="button">
  17. <!-- //子组件 -->
  18. <label for="username">用户名:</label>
  19. <input type="text" name="username" v-model="username" />
  20. <label for="password">密码:</label>
  21. <input type="text" name="password" v-model="pwd" />
  22. </template>
  23. <script>
  24. const app = Vue.createApp({
  25. data() {
  26. return {
  27. name: "admin",
  28. pwd: "123456",
  29. };
  30. },
  31. components: {
  32. myButton: {
  33. template: "#button",
  34. // props: 用数组接收父组件传入的自定义属性做为载体的参数
  35. props: ["username", "pwd"],
  36. },
  37. },
  38. });
  39. app.mount("#app");
  40. </script>
  41. </body>
  42. </html>

子组件 向父组件传递参数

  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. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div id="app">
  12. <my-button @click-info="fun"></my-button>
  13. </div>
  14. <template id="content">
  15. <input type="text" name="uname" v-model="name" placeholder="用户名" />
  16. <input type="text" name="pwd" v-model="pwd" placeholder="密码" />
  17. <!-- 发布订阅 -->
  18. <!-- $emit(自定义事件名称, 向父组件传递的参数(可选)) -->
  19. <!-- $emit('reviewCount', this.count) -->
  20. <button @click="$emit('click-info',[name,pwd])">提交</button>
  21. </template>
  22. <script>
  23. const app = Vue.createApp({
  24. methods: {
  25. fun(info) {
  26. console.log(info);
  27. },
  28. },
  29. });
  30. //全局组件
  31. app.component("my-button", {
  32. template: "#content",
  33. data() {
  34. return {
  35. name: "admin",
  36. pwd: "12345",
  37. info: {},
  38. };
  39. },
  40. });
  41. app.mount("#app");
  42. // 1. 父->子: props: [....]
  43. // 2. 子->父:
  44. // 2.1 子发布事件: $emit(自定义事件,参数?)
  45. // 2.2 父订阅事件: @自定义事件(...args)
  46. </script>
  47. </body>
  48. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议