美文网首页
vue3 组件传值

vue3 组件传值

作者: __鹿__ | 来源:发表于2022-05-23 16:00 被阅读0次

一、父传子接
通过 props接受,在vue3里props是响应式的


image.png

二、子传父接
通过emit,由于vue3中的setup的this指向是 undefined。所以使用setup的第二个参数(context)中的emit属性去传值。


image.png

原始页面


image.png

点击改变数据后的页面


image.png

附上完整代码:
home.vue (父组件)

<template>
  <div class="box">
    <div style="border: 1px solid red" class="style">
      父组件区域
      <div>
        <p>{{ name }}</p>
        <input type="text" name="" v-model="name" id="" />
      </div>
    </div>
    <div style="border: 1px solid green" class="style">
      子组件区域
      <InFo
        :name="name"
        :sex="about.sex"
        :height="about.height"
        
        @hello="showHelloMsg"
      ></InFo>
    </div>
  </div>
</template>
<script>
import { ref, reactive, onMounted, toRefs } from "vue";
import InFo from "../components/info.vue";
export default {
  components: {
    InFo,
  },
  setup(props, context) {
    console.log(props, context);
    let name = ref("小明");
    let about = reactive({
      sex: "男",
      height: 180,
    });
    function showHelloMsg(data) {
      name.value = "小红";
      about.sex = "女";
      about.height = 160;
      console.log("子组件传来的数据", data);
    }
    onMounted(() => {
      console.log("页面加载时");
    });
    return {
      name,
      about,
      showHelloMsg,
    };
  },
};
</script>
<style scoped>
.box{
    display: flex;
    justify-content: space-between;
}
.style {
  width: 49%;
  text-align: center;
  padding: 16px 0px;
}
</style>

info.vue (子组件)

<template>
  <div>
    <div>
      <p>姓名:{{ name }}</p>
      <p>性别:{{ sex }}</p>
      <p>身高:{{ height }}</p>
      <button @click="hello">改变数据</button>
    </div>
  </div>
</template>
<script>
import { ref, reactive, h, onMounted, toRefs } from "vue";
export default {
  props: {
    //父传子接
    name: String,
    sex: String,
    height: Number,
  },
  emits: ["hello"], // 要声明接收到了hello事件,否则会报警告
  setup(props, context) {
    let { name, sex, height } = toRefs(props);
    let { attrs, slots, emit } = context;

    console.log(name.value);
    console.log(sex.value);
    console.log(height.value);

    function hello() {
      //调用父组件的方法并传值
      emit("hello", 666);
      //context.emit("hello", 666);
    }
    onMounted(() => {
      console.log("页面加载时");
    });
    return {
      hello,
    };
  },
};
</script>

相关文章

  • (VUE3) 四、组件传值(父子组件传值 & 祖孙组件传值 &v

    1.父子组件传值 vue2中的父子组件传值:父组件: 子组件: vue3中的父子组件传值: 还是用props接收父...

  • Vue3(四)组件传值

    坚持、坚持、再坚持!努力、努力、再努力!今天把Vue3更完 关键字:父子组件传值、 1. 父子组件传值 父 子 2...

  • 前端VUE3,JQ,uniapp,综合

    vue3 + ts 子组件更新props 子组件可以直接修改父组件传进来的值子组件定义事件名称update:事件名...

  • vue3 - 父子组件之间的传值 2022-03-01

    近期学习 vue3 的父子组件之间的传值,发现跟 vue2.x的父子组件之间的传值 并没有太大的区别,我这边总结一...

  • Vue - 传值

    Vue 传值有两种:1)父组件向子组件传值(属性传值,Prop传值)2)子组件向父组件传值 一、父组件向子组件传值...

  • 组件之间的传值

    组件之间的传值,包括父子组件传值,兄弟组件之间的传值,其中父子组件包括父组件向子组件传值和子组件向父组件传值,现在...

  • Vue_组件间传值

    1、父组件传值给子组件2、子组件传值给父组件 1、父组件传值给子组件 2、子组件传值给父组件

  • vue3 组件传值

    一、父传子接通过 props接受,在vue3里props是响应式的 二、子传父接通过emit,由于vue3中的se...

  • 2018-09-05

    组件传值问题 父组件给子组件传值应该使用props。子组件要给父组件传值,需要调用父组件传递的方法。props传值...

  • vue2.0 父子组件传值

    父组件传值给子组件 子组件 子组件通过 props 接收值 父组件 父组件通过标签属性传值 子组件传值给父组件 子...

网友评论

      本文标题:vue3 组件传值

      本文链接:https://www.haomeiwen.com/subject/leirprtx.html