Blogger Information
Blog 50
fans 0
comment 0
visits 31538
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue组件及通信方式
手机用户1580651468
Original
456 people have browsed it

Vue组件及通信方式

一. 声明组件与子组件,以及他们之间的通信方式

一). 父-子:自定义属性

1.代码

  1. <body>
  2. <!-- 挂载点 -->
  3. <div id="app"></div>
  4. <!-- 父组件的模板代码 -->
  5. <template id="parent">
  6. <counter username="admin" email="496542445@qq.com" />
  7. </template>
  8. <!-- 子组件的模板代码 -->
  9. <template id="child">
  10. <p>UserName:{{username}}</p>
  11. <p>UserEmail:{{email}}</p>
  12. </template>
  13. <script>
  14. const app = Vue.createApp({
  15. // 父组件
  16. template: `#parent`,
  17. });
  18. app.component("counter", {
  19. // 接受父组件的传过来的参数
  20. props: ["username", "email"],
  21. template: `#child`,
  22. });
  23. const vm = app.mount("#app");
  24. </script>
  25. </body>

2.实现效果图

二).子-父:自定义事件

1.代码

  1. <body>
  2. <!-- 挂载点 -->
  3. <div id="app">
  4. <counter @review="review" />
  5. </div>
  6. <template id="counter">
  7. <button @click="count++">点赞:+{{count}}</button>
  8. <button @click="review()">评论</button>
  9. </template>
  10. <script>
  11. const app = Vue.createApp({
  12. methods: {
  13. review(count) {
  14. console.log(count);
  15. },
  16. },
  17. });
  18. app.component("counter", {
  19. template: `#counter`,
  20. data() {
  21. return {
  22. count: 0,
  23. };
  24. },
  25. methods: {
  26. review() {
  27. this.$emit("review", this.count);
  28. },
  29. },
  30. });
  31. const vm = app.mount("#app");
  32. // 1. 父-子:自定义属性
  33. // 2.子-父:自定义事件
  34. </script>
  35. </body>

2.实现的效果图

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