Blogger Information
Blog 94
fans 0
comment 0
visits 91871
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【Vue】Vue 组件的定义及组件之间的通信
可乐随笔
Original
1483 people have browsed it

Vue 组件的定义及组件之间的通信

1. 组件的注册

总结:

1.挂载点
2.创建VUE组件实例(根组件)
3.创建全局组件
4.声明子组件及孙组件
5.组件与挂载点绑定

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@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <!-- 挂载点 -->
  12. <div id="app">
  13. <!-- 组件挂载 -->
  14. <greeting />
  15. </div>
  16. <script>
  17. // 应用实例
  18. const app = Vue.createApp({
  19. // 根组件配置项
  20. });
  21. // 全局组件(父组件)
  22. // app.component(组件名称,{组件配置项})
  23. app.component('greeting', {
  24. // html模板
  25. template: `
  26. <h2>Hello, {{uname}}</h2>
  27. <button @click='setLike'>点赞 + {{count}}</button>
  28. <!-- 命名规则: 小驼 -> 肉串 -->
  29. <my-award v-if='count >= 10' />
  30. `,
  31. // 数据
  32. data() {
  33. return {
  34. uname: '王老师',
  35. count: 0,
  36. };
  37. },
  38. // 方法
  39. methods: {
  40. setLike() {
  41. this.count++;
  42. },
  43. },
  44. // 声明子组件(复数属性)
  45. components: {
  46. // 奖励组件
  47. myAward: {
  48. template: `<p>实在太受欢迎了</p>`,
  49. data() {
  50. return {};
  51. },
  52. components: {
  53. // 声明子的子组件
  54. // userName: 小驼
  55. // last-user-name : 肉串
  56. },
  57. },
  58. },
  59. });
  60. // 根组件实例 (vue应用与挂载点绑定)
  61. const vm = app.mount('#app');
  62. </script>
  63. </body>
  64. </html>

2. 向子组件值参:属性

总结:

props:用于接收父组件中传来的参数(用的是自定义属性)

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@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <div id="app"></div>
  12. <!-- 父组件的模板 -->
  13. <template id="parent">
  14. <counter uname='admin' email='nx99@qq.com'>
  15. </template>
  16. <!-- 子组件模板:必须使用ID选择符 -->
  17. <template id="child">
  18. <p>UserName:{{uname}}</p>
  19. <p>UserEmail:{{email}}</p>
  20. </template>
  21. <script>
  22. const app = Vue.createApp({
  23. //父组件
  24. template: `#parent`,
  25. });
  26. app.component('counter', {
  27. //子组件
  28. // props:用于接收父组件中传来的参数(用的是自定义属性)
  29. props: ['uname', 'email'],
  30. template: `#child`,
  31. })
  32. const vm = app.mount('#app');
  33. </script>
  34. </body>
  35. </html>

3. 向父组件值参:自定义事件

总结:

  1. 父 -> 子:自定义属性
  2. 子 -> 父:自定义事件

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@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <!--
  12. 1. 父 -> 子:自定义属性
  13. 2. 子 -> 父:自定义事件
  14. -->
  15. <div id="app">
  16. <counter @review='review'>
  17. </div>
  18. <template id="counter">
  19. <button @click="count++">点赞 + {{count}}</button>
  20. <button @click="review()">评论</button>
  21. </template>
  22. <script>
  23. const app = Vue.createApp({
  24. methods: {
  25. review(count) {
  26. console.log(count);
  27. if (count >= 10) {
  28. alert('可以下课了');
  29. };
  30. },
  31. },
  32. });
  33. app.component('counter', {
  34. template: `#counter`,
  35. data() {
  36. return {
  37. count: 5,
  38. }
  39. },
  40. methods: {
  41. review() {
  42. this.$emit('review', this.count);
  43. },
  44. },
  45. })
  46. const vm = app.mount('#app');
  47. </script>
  48. </body>
  49. </html>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!