• Vue组件(二)父组件、子组件通信/传值


    一、Vue 父组件访问子组件

    1.父组件获取子组件对象

    通过ref引用属性访问子组件对象

    定义:

     <StudentInfo :num="parentNum" ref="parentStu">StudentInfo>

    使用:

    1. //获取子组件信息
    2. console.info(this.$refs["parentStu"]);

     

    2.父组件调用子组件方法

    1. //父组件调用子组件方法
    2. this.$refs["parentStu"].change();

    3.父组件传值给子组件 props  (重点,可以双向绑定)

    1.子组件定义

    1. props: {
    2. num: {
    3. type: Number,
    4. default: 10,
    5. },
    6. },

    2.父组件使用,重点parentNum 可以实现双向绑定

    1. <StudentInfo :num="100">StudentInfo>
    2. <StudentInfo :num="parentNum" ref="parentStu">StudentInfo>

    二、Vue 子组件访问父组件

    1.获取父组件对象(重点,直接修改父组件变量,可以修改父组件页面展示)

    this.$root
    1. //子组件获取父组件数据
    2. this.$root.parentNum += 10;

    2.调用父组件方法

    1. //子组件调佣父组件 方法
    2. this.$root.parentChange();

    3.给父组件传值

    方案:1.调用子组件方法传值  2.使用props双向绑定传值

    三、源码如下:

    子组件:

    1. <template>
    2. <div>
    3. <p>编号:{{ stu.id }}p>
    4. <p>姓名:{{ stu.name }}p>
    5. <p>测试数字:{{ num }}p>
    6. <button @click="childClick">子组件按钮button>
    7. div>
    8. template>
    9. <script>
    10. export default {
    11. data() {
    12. return {
    13. stu: {
    14. id: 1,
    15. name: "张三丰",
    16. },
    17. };
    18. },
    19. props: {
    20. num: {
    21. type: Number,
    22. default: 10,
    23. },
    24. },
    25. methods: {
    26. change() {
    27. this.stu = {
    28. id: 2,
    29. name: "李四",
    30. };
    31. },
    32. //子组件事件
    33. childClick() {
    34. console.info(this);
    35. //子组件获取父组件数据
    36. this.$root.parentNum += 10;
    37. //子组件调佣父组件 方法
    38. this.$root.parentChange();
    39. },
    40. },
    41. mounted() {},
    42. };
    43. script>

    父组件:

    1. <template>
    2. <div>
    3. <img alt="Vue logo" src="./assets/logo.png" />
    4. <StudentInfo :num="100">StudentInfo>
    5. <StudentInfo :num="parentNum" ref="parentStu">StudentInfo>
    6. <button @click="changeInfo">父组件按钮button>
    7. <p>
    8. parentNum:{{parentNum}}
    9. p>
    10. div>
    11. template>
    12. <script>
    13. import StudentInfo from "./components/StudentInfo.vue";
    14. export default {
    15. name: "App",
    16. components: {
    17. StudentInfo,
    18. },
    19. data() {
    20. return {
    21. parentNum: 88,
    22. };
    23. },
    24. methods: {
    25. changeInfo() {
    26. //获取子组件信息
    27. console.info(this.$refs["parentStu"]);
    28. //父组件调用子组件方法
    29. this.$refs["parentStu"].change();
    30. //父组件修改子组件数据
    31. this.parentNum = 99;
    32. },
    33. parentChange(){
    34. this.parentNum++;
    35. }
    36. },
    37. };
    38. script>
    39. <style>
    40. #app {
    41. font-family: Avenir, Helvetica, Arial, sans-serif;
    42. -webkit-font-smoothing: antialiased;
    43. -moz-osx-font-smoothing: grayscale;
    44. text-align: center;
    45. color: #2c3e50;
    46. margin-top: 60px;
    47. }
    48. style>

    更多:

    Vue组件(一)Vue组件基础_Vue组件入门

    Vue3实现复制功能_vue-clipboard3 Vu3复制插件

    Vue 无法展示网络图片处理方案

  • 相关阅读:
    cartographer-(0)-ubuntu(20.04)-环境安装
    Spring Boot项目优雅实现读写分离
    Mysql on duplicate key update用法,注意三点就能上手
    AI大模型探索之路-训练篇4:大语言模型训练数据集概览
    【数据结构】堆排序和top-K问题
    Django框架web开发实战:Model和ORM学习(三)
    数据集成测试支持工具
    自学软件测试?先学完这些就已经够了....
    CesiumJS 2022^ 原理[2] 渲染架构之 Primitive - 创建并执行指令
    AVL树 c语言版本 插入部分
  • 原文地址:https://blog.csdn.net/u011127019/article/details/127104123