vue组件之间的传值

一. emit传值(全局data中的值传递)

// main.js文件定义eventHub值
 data: {
    eventHub: new Vue(),
  },
// 设置传入全局eventHub中的值,getCardAccount为自定义函数 getCardAccount中的值为 111
 this.$root.eventHub.$emit(
    "getCardAccount",
    111
  );
// 设置模块下getCardAccount方法为传入getCardAccount函数
 this.$root.eventHub.$on("getCardAccount", this.getCardAccount);
 
// 获取main.js全局值,通过getCardAccount函数获取设置的值
 getCardAccount(val) {
 // val = 111 
    this.roomCard = val;
  },

二. $parent

// routeer/index.js文件

import HelloWorld from '@/components/HelloWorld'
import storeManue from '@/components/storeManue'
// 路由定义模块关系 父级文件指向 / 二级指向 /store/index
const router = new Router({
 routes: [{
  path: '/',
   name: 'HelloWorld',
   redirect: '/store/index',
   component: HelloWorld,
   children: [{
     path: '/store/index',
     name: 'storeManue',
     component: storeManue,
   },{
     path: '/store/buyProducts',
     name: 'buyProducts',
     component: buyProducts,
   },{
     path: '/store/productCar',
     name: 'productCar',
     component: productCar,
    }],
    }
  ]
// HelloWorld.vue文件下(父级组件)
<template>
  <div class="hello">
    <div class="demo-content-block" v-resize="resize" :style="{height:bottomHeight}">
      <router-view></router-view>
    </div>
    <mu-container>
      <mu-bottom-nav :value.sync="shift" >
        <mu-bottom-nav-item value="home" title="首页" @click.native="storeManue" icon="home"></mu-bottom-nav-item>
    </mu-container>
  </div
</template>
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      shift: 'home',
      bottomHeight: '',
    }
  },
}
</script>

//   子级组件获取值或者重新设置父级值
<template>
  <div>
    storeManue
  </div>
</template>

<script>
  export default {
    data(){
      return {

      };
    },
    created(){
		// 获取父级组件的data值  
		console.log(this.$parent.shift)
		// 设置父级组件值
		this.$parent.shift =111;
    },
  }
</script>

三. props (与第二种基本相同,只是传值方式获取方式不同)

// routeer/index.js文件

import HelloWorld from '@/components/HelloWorld'
import storeManue from '@/components/storeManue'
// 路由定义模块关系 父级文件指向 / 二级指向 /store/index
const router = new Router({
 routes: [{
  path: '/',
   name: 'HelloWorld',
   redirect: '/store/index',
   component: HelloWorld,
   children: [{
     path: '/store/index',
     name: 'storeManue',
     component: storeManue,
   },{
     path: '/store/buyProducts',
     name: 'buyProducts',
     component: buyProducts,
   },{
     path: '/store/productCar',
     name: 'productCar',
     component: productCar,
    }],
    }
  ]
// HelloWorld.vue文件下(父级组件)
<template>
  <div class="hello">
    <div class="demo-content-block" v-resize="resize" :style="{height:bottomHeight}">
      <router-view :shift="shift"></router-view>
    </div>
    <mu-container>
      <mu-bottom-nav :value.sync="shift" >
        <mu-bottom-nav-item value="home" title="首页" @click.native="storeManue" icon="home"></mu-bottom-nav-item>
    </mu-container>
  </div
</template>
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      shift: 'home',
      bottomHeight: '',
    }
  },
}
</script>

//   子级组件获取值
<template>
  <div>
    storeManue
  </div>
</template>

<script>
  export default {
  	props: ['shift'],
    data(){
      return {

      };
    },
    created(){
		// 获取父级组件的data值  
		console.log(this.shift)
    },
  }
</script>

四. 独立组件之间的传值(通过文件引用使用独立文件的组件)

// 独立组件 tips.vue文件

<template>
  <div>
    <p class="warning-tip" id="warning_tip" :style="{display:show}">{{message}}</p>
  </div>
</template>

<script>
  export default{
    data(){
      return{
        show:'none',
      }
    },
    props:['isShow','message','time'],
    watch:{
      isShow(newval){
        console.log(newval)
        if(newval){
          this.warningTip(this.time);
        }
      }
    },
    methods:{
      warningTip(time){
        this.show = 'block';
        setTimeout(()=>{
          this.show = 'none';
          // 返回函数
          this.$emit('child-say', false);
        }, time)
      }
    }
  }
</script>

<style>
  .warning-tip {
    position: absolute;
    z-index: 1000;
    top: 52%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    padding: 9px 20px 7px;
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    border-radius: 4px;
    white-space: nowrap;
    margin: 0;
  }
</style>



// 需要使用tips组件的文件
<template>
  <div>
    <tips :isShow="isShow" :message="message" :time="time" v-on:child-say="childSay"></tips>
  	<button @click="deleteNumber"></button>
  </div>
</template>

<script>
  import tips from '@/components/tips'
  export default {
    components: { tips },
    data() {
      return {
        time: '',
        message: '',
        isShow: false
      }
    },
    created() {
    },
    methods: {
      childSay(){
        this.isShow = false;
      },
      deleteNumber(){
         this.isShow = true;
         this.message = '选购数量不能小于1件';
         this.time = '2000';
      
      },
    },
  }
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue组件之间传值Vue框架中的一个重要功能,它允许不同的组件之间共享数据,实现数据的传递和交互。下面是Vue组件之间传值的最全面解释: 1. 父组件向子组件传值:父组件通过props属性向子组件传递数据,子组件通过props属性接收父组件传递的数据。这种方式适用于父子组件之间的数据传递,父组件可以向多个子组件传递数据。 2. 子组件向父组件传值:子组件通过$emit方法触发事件,父组件通过v-on指令监听事件,并通过事件对象获取子组件传递的数据。这种方式适用于子组件向父组件传递数据,子组件可以触发多个事件,父组件可以监听多个事件。 3. 兄弟组件之间传值:使用一个共同的父组件作为中介,通过props属性和$emit方法实现兄弟组件之间的数据传递。这种方式适用于兄弟组件之间的数据共享,需要注意父组件的数据传递和事件监听。 4. 使用Vuex进行状态管理:Vuex是Vue的状态管理库,可以实现多个组件之间的数据共享和状态管理。通过Vuex的store对象存储数据和状态,并通过mutations、actions和getters等方法实现数据的修改和获取。 5. 使用事件总线:通过Vue的事件机制,创建一个事件总线,多个组件可以通过事件总线进行数据传递。通过Vue实例的$emit方法触发事件,通过Vue实例的$on方法监听事件,并通过事件对象获取数据。 以上是Vue组件之间传值的最全面解释,需要根据具体的场景选择不同的传值方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值