vue:父子组件传值

一、父组件传值给子组件

父组件调用子组件时绑定自定义属性,子组件通过props接收。

二、子组件传值给父组件

1、父组件调用子组件时自定义事件,子组件传值时触发该自定义事件。

2、父组件调用子组件时绑定自定义属性,属性是一个函数,子组件传值时直接调用该函数进行传值。

三、代码示例

代码中,App.vue是父组件,One.vue、Two.vue、Three.vue是子组件

它们之间的传值关系:

App.vue传值给One.vue

Two.vue传值给App.vue

Three.vue传值给App.vue

App.vue

<template>
    <div id="app">
    
        <input type="text" ref="in" />
        <button @click="sendAction">按钮</button>
    
        <hr/>
    
        <!-- 父组件传值给子组件 -->
        <!-- 父组件调用子组件时绑定自定义属性,子组件通过props接收。 -->
        <one-com :val="value" />
    
        <hr/>
        
        <!-- 子组件传值给父组件 -->
        <!-- 父组件调用子组件时自定义事件,子组件传值时触发该自定义事件。 -->
        <p>接收到two的值为:{{twoData}}</p>
    
        <two-com @send="handleSend" />
    
        <hr/>
    
        <!-- 子组件传值给父组件 -->
        <!-- 父组件调用子组件时绑定自定义属性,属性是一个函数,
             子组件传值时直接调用该函数进行传值。 -->
        <p>接收到three的值为:{{threeData}}</p>
    
        <three-com :sendAction="handleThreeData" />
      
    </div>
</template>

<script>
import One from './components/One'
import Two from './components/Two'
import Three from './components/Three'
export default {
    data(){
        return {
            value: 'test',
            twoData: null,
            threeData: null
        }
    },
    methods: {
        sendAction(){
            this.value = this.$refs.in.value;
        },
        handleSend(val){
            this.twoData = val;
        },
        handleThreeData(val){
            this.threeData = val;
        }
    },
    components: {
        'one-com': One,
        'two-com': Two,
        'three-com': Three
    }
}
</script>

<style>

</style>

One.vue

<template>
    <div class="one">

        <p>接收到的值为:{{ val }}</p>

    </div>
</template>

<script>
export default {
    props: ['val']
}
</script>

<style>

</style>

Two.vue

<template>
    <div class="two">

        <input type="text" ref="in" />
        <button @click="btnAction">发送</button>

    </div>
</template>

<script>
export default {
    methods: {
        btnAction(){
            let val = this.$refs.in.value;
            // 触发父组件绑定的自定义send事件
            this.$emit('send', val);
        }
    }
}
</script>

<style>

</style>

Three.vue

<template>
    <div class="three">

        <input type="text" ref="in" />
        <button @click="btnAction">发送</button>

    </div>
</template>

<script>
export default {
    props: {
        sendAction: Function
    },
    methods: {
        btnAction(){
            let val = this.$refs.in.value;
            // 直接调用sendAction函数进行传值
            this.sendAction(val);
        }
    }
}
</script>

<style>

</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 2中,父子组件之间的数据递是双向的,主要通过props(属性)和$emit(事件)来实现。以下是一些常见的父子组件方法: 1. **Props(属性)**: - **单向数据流**:父组件向子组件递数据使用`props`。父组件定义props时,需要指定类型,子组件通过`props`接收数据。例如: ```javascript // 父组件 <child-component :value="parentValue" /> // 子组件 props: { value: { type: String, default: '' } } ``` 子组件只能读取props,不能修改,除非父组件通过$emit触发自定义事件。 2. **自定义事件($emit)**: - **父子组件间的双向通信**:如果需要子组件向父组件发送更新,可以使用$emit触发一个自定义事件,如: ```javascript // 子组件 this.$emit('update-value', newValue); // 父组件 <child-component @update-value="handleChildValueChange" /> methods: { handleChildValueChange(value) { this.parentValue = value; } } ``` 3. **引用类型(Object/Array)**: - 如果父组件递对象或数组,Vue会默认浅复制。若需要深拷贝,需在子组件中处理,例如使用`Vue.extend`或`JSON.parse(JSON.stringify())`。 4. **自定义指令(v-bind、v-on)**: - 可以使用自定义指令如`v-model`在组件间共享数据,但这在Vue 2中不是推荐的做法,因为可能会带来性能问题。 5. **$parent/$children/$refs**: - 有时可能需要直接访问父组件或子组件的实例,这时可以使用`$parent`、`$children`和`$refs`。但这些选项通常不鼓励滥用,因为它们破坏了组件的封装性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值