【Vue2和Vue3父子组件传值】

Vue2中的父子组件传参:

父传子

props:在父组件中,给子组件绑定一个自定义属性,在子组件中,通过props进行接收

子传父

自定义事件:在父组件中,给子组件绑定一个自定义事件,绑定事件的值为接收参数的函数,在子组件中,通过$emit发送数据

Vue3中的父子组件传参:

vue3中,新增了setup语法糖,父组件中引入子组件后,不需要注册可直接使用。父传子时,子组件中通过defineProps方法接收,子传父时,子组件中通过defineEmits方法发送。

Vue2父子组件传值

vue2父传子

在父组件中的子标签中定义一个 :属性名=属性值,在子组件中用props接收

父组件:
<template>
  <div>
    <div>父组件</div>
    <sonOne :msg="msg"></sonOne>
  </div>
</template>

<script>
import sonOne from "@/components/sonOne";

export default {
  name: "fatherView",
  components:{
    sonOne
  },
  data(){
    return{
      msg:'父组件传到子组件的值'
    }
  }

}
</script>

<style scoped>

</style>
子组件:
<template>
<div>
  子组件
  {{msg}}
</div>
</template>

<script>
export default {
name: "sonOne",
  props:{
    msg:[String,Number],
  },
  created() {
    console.log(this.msg)
  }
}
</script>

<style scoped>

</style>

vue2子传父

在父组件的子标签中写自定义事件,在子组件中用this.$emit(‘自定义事件名’,传递的值)传值

父组件:
<template>
  <div>
    父组件
    <sonTwo @change="change"></sonTwo>
  </div>
</template>

<script>
import sonTwo from "@/components/sonTwo";

export default {
  name: "fatherOneView",
  components: {
    sonTwo
  },
  methods:{
    change(data){
      console.log(data)
    }
  }
}
</script>

<style scoped>

</style>
子组件:
<template>
<div>
  子组件
  <button @click="click">按钮</button>
</div>
</template>

<script>
export default {
  name: "sonTwo",
  created() {

  },
  methods:{
    click(){
      this.$emit('change','子组件传到父组件的值')
    }
  }
}
</script>

<style scoped>

</style>

Vue3父子组件传参

Vue3父传子

在父组件中的子标签中定义一个 :属性名=属性值,在子组件中用defineProps接收

父组件:
<template>
  <div>
    父组件
    <sonOne :msg="msg"></sonOne>
  </div>
</template>

<script setup lang="ts">
import sonOne from '../components/sonOne.vue'
import {ref} from 'vue'
let msg = ref('父组件传给子组件的值')
</script>

<style scoped>

</style>
子组件:
<template>
  <div>
    子组件
    {{ msg }}
  </div>
</template>

<script setup lang="ts">
defineProps<{
  msg:[]
}>()

</script>

<style scoped>

</style>

Vue3子传父

在父组件的子标签中写自定义事件,在子组件中写一个点击事件,再定义emit,emit的第一个参数是父组件的方法,第二参数就是需要传递的值

父组件:
<template>
  <div>
    父组件
    <sonTwo @change="change">按钮</sonTwo>
    {{msg}}
  </div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import sonTwo from "../components/sonTwo.vue";
let msg = ref('')
const change = (data) => {
  console.log(data);
  msg.value = data
};
</script>

<style scoped>

</style>
子组件:
<template>
  <div>
    子组件
    <button @click="send">按钮</button>
  </div>
</template>

<script setup lang="ts">

const emit = defineEmits(['change'])
const send=()=>{
  emit('change','子组件传给父组件的值')
}

</script>

<style scoped>

</style>
  • 4
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3中,父子组件之间传值可以通过props和emit来实现。下面是一种常见的父子组件传值方式: 1. 父组件通过props向子组件传递数据: 在父组件中,可以通过在子组件标签上绑定属性的方式将数据传递给子组件。例如: ```html <template> <div> <child-component :message="parentMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: 'Hello from parent component' }; } }; </script> ``` 在子组件中,可以通过props接收父组件传递的数据。例如: ```html <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: { message: { type: String, required: true } } }; </script> ``` 这样,父组件中的`parentMessage`会传递给子组件的`message`属性。 2. 子组件通过emit向父组件传递数据: 在子组件中,可以通过`$emit`方法触发一个自定义事件,并将需要传递给父组件的数据作为参数传递。例如: ```html <template> <div> <button @click="sendMessage">Send Message to Parent</button> </div> </template> <script> export default { methods: { sendMessage() { this.$emit('message-to-parent', 'Hello from child component'); } } }; </script> ``` 在父组件中,可以通过在子组件标签上监听自定义事件的方式接收子组件传递的数据。例如: ```html <template> <div> <child-component @message-to-parent="handleMessageFromChild"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { handleMessageFromChild(message) { console.log(message); // 输出:Hello from child component } } }; </script> ``` 这样,子组件中通过`$emit`方法触发的`message-to-parent`事件会被父组件中的`handleMessageFromChild`方法捕获,并将子组件传递的数据作为参数传递给该方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值