一.父子组件传值 

         要点1:父组件赋予子组件属性值,子组件通过props 来接收值。

        要点2:父组件可以通过 子组件对象($ref),来调用子组件的属性以及方法

        要点3:子组件通过$emit 来调用父组件的方法

        示例如下:

         parent.vue   

<template>
  <div>
    <div>This is parent Component</div>
    <div>
      <!-- 自定义属性a ,b ,c   .sync用于子组件  自定义事件名tt @相当于v-on !-->
      <childComponent a="1" b="2" :c.sync="para" @tt="testFunc"></childComponent>
    </div>
    <div>C的值是:{{para}}</div>
  </div>
</template>

<script>
import childComponent from "./child"; //引入一个组件
export default {
  name: "parentComponent",
  components: { childComponent }, //定义父子关系
  data() {
    return {
      para: 31
    };
  },
  mounted() {},
  methods: {
    testFunc(p) {
      alert("这是父页面的方法" + p);
    }
  }
};
</script>
<style lang="" scoped>
</style>
View Code

相关文章:

  • 2022-12-23
  • 2020-04-24
  • 2021-11-12
  • 2022-12-23
  • 2022-12-23
  • 2021-03-09
  • 2021-06-08
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2020-11-02
  • 2021-11-13
相关资源
相似解决方案