2021.4.22

父子组件间的数据传递及方法调用

  1. 父组件通过ref使用子组件的方法并修改子组件数据
  2. 子组件通过.$parent使用父组件的方法动态修改父组件信息
  3. 子组件通过watch监听父组件数据变化

parent.vue

<template>
  <div class="container">
    <input
      v-model="childrenValue"
      placeholder="parent"
      @change="changeChildData"
    >

    <hr />

    <input
      v-model="obj.name"
      placeholder="parent"
    >

    <hr />

    <children
      ref="children"
      :parentData="childrenValue"
      :ObjData="obj"
    />

    我是祖先的值: {{grandsonValue}}

    <hr />

    <brother :ObjData="obj" />
  </div>
</template>

<script>
import children from "./children";
import brother from "./brother";

export default {
  data() {
    return {
      grandsonValue: {
        grandsonValueName: "",
        grandsonValueValue: ""
      },
      childrenValue: "",
      obj: {
        name: "name",
        age: 18
      }
    };
  },
  watch: {
    childrenValue: function(newValue) {
      console.log("后代值发生了改变");
    }
  },
  provide() {
    // 祖先注册
    return {
      appmy: this
    };
  },
  components: { children, brother },
  mounted() {
    this.getChildMethod();
  },
  methods: {
    changeChildData() {
      this.$refs.children.childrenData.name = this.childrenValue;
    },

    getChildMethod() {
      this.$refs.children.childrenMethod("我在父组件使用"); // 在父组件中调用
      console.log(this);
      console.log(
        "我在父组件拿到了子组件的数据:" + this.$refs.children.childrenData.name
      );
    },

    ancestorMethod(info) {
      console.log("我是祖先组件方法," + info);
    },

    parentMethod(info) {
      console.log("我是父组件方法," + info);
    }
  }
};
</script>

children.vue

<template>
  <div class="container">
    我是子组件的数据(被父组件使用):{{childrenData}}
    <hr />
    我是子组件的数据(对象):{{ObjData}}
    <hr />
    我在子组件,下面是孙子组件:
    <grandson ref="grandson" />
  </div>
</template>

<script>
import grandson from "./grandson";

export default {
  props: ["parentData", "ObjData"],
  data() {
    return {
      childrenData: {
        name: "children"
      }
    };
  },
  watch: {
    parentData: function(newValue) {
      console.log(
        "父组件传给子组件的值发生了改变,如果传入值为对象,改变对象的属性值并不会触发watch"
      );
    },
    ObjData: function(newValue) {
      console.log("改变对象的属性值并不会触发watch");
    }
  },
  components: { grandson },
  mounted() {
    this.getParentMethod();
  },
  methods: {
    getParentMethod() {
      this.$parent.parentMethod("我在子组件中使用");
      console.log("我在子组件拿到了父组件的数据:" + this.$parent.obj.name);
    },

    childrenMethod(info) {
      console.log("我是子组件方法," + info);
    }
  }
};
</script>

<style scoped lang="scss">
</style>
  1. 兄弟组件获取数据和方法

brother.vue

<template>
  <div class="container">
    我是children 的 brother , 我通过修改parant 传入childen的对象,改变childen的值
    <input
      v-model="ObjData.name"
      placeholder="brother"
    >
  </div>
</template>

<script>
export default {
  props: ["ObjData"],
  data() {
    return {};
  },
  components: {},
  mounted() {
    this.getBrotherMethod();
  },
  methods: {
    getBrotherMethod() {
      this.$parent.$refs.children.childrenMethod("我在兄弟组件使用");
    }
  }
};
</script>

<style scoped lang="less">
</style>

  1. provide/inject实现后代组件获取祖先组件的数据和使用祖先组件的方法

    grandson.vue

    <template>
      <div>
        我拿到了祖先的值: {{appmy.obj}}
        <hr />
        我使用了祖先的方法:{{appmy.ancestorMethod("我在后代组件使用")}}
        我是后代组件的输入框, 我改变祖先组件的数据:
        <input
          v-model="appmy.grandsonValue.grandsonValueValue"
          placeholder="grandson"
        >
      </div>
    </template>
    
    <script>
    export default {
      // 后代使用
      inject: ["appmy"],
      data() {
        return {};
      },
      components: {}
    };
    </script>
    
    <style scoped lang="less">
    </style>
    
  2. 通过$refs 层层获取

  3. 使用eventBus 共用方法

对象的深度克隆

JSON.parse(JSON.stringify(obj)) //得到存储地址改变的对象

ref获取不到指定的DOM节点问题

this.$nextTick(function() {
  console.log(this.$refs.mysql);
}); // 等dom组件加载完毕再调用

eg:
    async conn(data) {
      let preData = JSON.parse(JSON.stringify(data));
      await this.$nextTick(function() {
        preData.password = this.$refs.mysql.checkPwd(preData.password);
      });
      console.log(preData.password);

      const url = "/v1/api/data/connect";
      axios.request(url, "get", preData, true).then(res => {
        this.tableSource = res;
        this.$emit("connResp", res);
      });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要将数据转换成pandas的DataFrame格式,代码如下: ```python import pandas as pd data = {'日期/项目(A厂)': ['2021.1.1', '2021.1.2', '2021.1.3', '2021.1.4', '2021.1.5', '2021.1.6', '2021.1.7', '2021.1.8', '2021.1.9', '2021.1.10', '2021.1.11', '2021.1.12', '2021.1.13', '2021.1.14', '2021.1.15', '2021.1.16', '2021.1.17', '2021.1.18', '2021.1.19', '2021.1.20', '2021.1.21', '2021.1.22', '2021.1.23', '2021.1.24', '2021.1.25', '2021.1.26', '2021.1.27', '2021.1.28', '2021.1.29', '2021.1.30', '2021.1.31'], '进水': [149, 164, 86, 164, 146, 136, 93, 96, 90, 134, 141, None, None, None, 138, 138, 161, None, None, None, None, None, None, None, None, None, 114, 107, 121, None, None], 'COD': [20.1, 10.1, 37.1, 16.4, 10.9, 18.7, 17.2, 17.1, 18.5, 23.8, 17.7, 15.6, 11.0, 19.5, 18.5, 15.2, 16.5, 16.3, 17.3, 29.5, 20.7, 19.5, 18.9, 12.0, 23.9, 11.7, 10.6, 11.1, 14.2, 10.6, 12.5], '氨氮': [3.54, 0.65, 1.92, 1.44, 0.84, 1.59, 1.15, 1.61, 1.42, 2.46, 2.50, 1.48, 1.04, 3.55, 1.60, 1.82, 2.60, 2.10, 1.54, 3.54, 2.67, 3.25, 2.12, 2.38, 2.34, 1.51, 1.58, 1.31, 1.66, 1.26, 1.71], '总磷': [30.7, 20.1, 44.1, 21.5, 18.4, 29.7, 23.5, 24.2, 26.9, 31.7, 28.3, None, None, 24.0, 26.9, 27.8, 20.5, 27.9, 31.8, 37.8, 24.9, 29.3, None, 23.4, 23.5, 12.4, 27.9, 19.3, 17.6, 19.5, 15.4]} df = pd.DataFrame(data) ``` 接下来,我们可以使用matplotlib库进行数据可视化,这里我选择绘制折线图。代码如下: ```python import matplotlib.pyplot as plt # 设置图形大小 plt.figure(figsize=(10, 6)) # 绘制折线图 plt.plot(df['日期/项目(A厂)'], df['进水'], label='进水') plt.plot(df['日期/项目(A厂)'], df['COD'], label='COD') plt.plot(df['日期/项目(A厂)'], df['氨氮'], label='氨氮') plt.plot(df['日期/项目(A厂)'], df['总磷'], label='总磷') # 添加标题和标签 plt.title('A厂水质监测', fontsize=16) plt.xlabel('日期', fontsize=12) plt.ylabel('含量', fontsize=12) # 添加图例 plt.legend() # 显示图形 plt.show() ``` 运行上述代码,即可得到一张含有4条曲线的折线图,用于展示A厂水质监测数据的趋势。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值