vue组件传值 父传子、子传父(3种)、非父子传值

父组件向子组件传值

直接在子组件中通过props来接收

父组件:

<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png" />
    <HelloWorld :obj1="obj" :mes1="mes"></HelloWorld>
  </div>
</template>

<script>
import HelloWorld from "@/components/HelloWorld.vue";

export default {
  name: "home",
  data() {
    return {
      obj: {
        name: "hello",
        age: 12,
        last: "父向子传的值"
      },
      mes: "我是父组件的内容",
    };
  },
  components: {
    HelloWorld
  }
};
</script>

 
子组件

<template>
  <div class="hello">
    <h1>{{ mes1 }}</h1>
    <h2>{{obj1.last}}</h2>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  props: {
    obj1: Object,
    mes1: String
  },
};
</script>

 
 
 

子组件向父组件传值(3种)

通过$.emit

父组件:

<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png" />
    <HelloWorld @setValue="getValue"></HelloWorld>
    <h1>{{value}}</h1>
  </div>
</template>

<script>
import HelloWorld from "@/components/HelloWorld.vue";

export default {
  name: "home",
  data() {
    return {
      value: [],
    };
  },
  components: {
    HelloWorld
  },
  methods: {
    getValue(value) {
      this.value = value;
    }
  },
};
</script>

子组件:

<template>
  <div class="hello">
    <h1 @click="change" ref="h1">just click me</h1>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      val: "子组建向父组件传的值"
    };
  },
  // mounted () {
  //   this.change();
  // },
  methods: {
    change(){
      this.$emit("setValue",this.val);
      this.$refs.h1.setAttribute('class','backcolor');
    }
  }
};
</script>

<style scoped lang="less">
.backcolor {
  color: red;
}
</style>

gif效果图:
在这里插入图片描述

 

通过ref属性

调用子组件方法时可以传值,ex:
this.$refs.xxx.someMeth(param)

父组件:

<template>
  <div>
    <h1>父组件</h1>
    <Child ref="child"></Child>
    <h4>{{mes}}</h4>
  </div>
</template>

<script>
import child from '../components/child'
export default {
  data() {
    return {
      mes: ''
    };
  },
  components:{
    Child: child
  },
  mounted() {
    this.mes = this.$refs.child.message;
  },
};
</script>
<style lang='less' scoped>
</style>

子组件:

<template>
  <div></div>
</template>

<script>
export default {
  data() {
    return {
        message: '我是子组件的内容'
    };
  },
};
</script>
<style lang='scss' scoped>
</style>

 

通过slot作用域插槽

ps:只要出现多个插槽,始终为所有的插槽使用完整的基于 template 的语法,即v-slot 只能添加在 <template> 上 并绑定相应的name,v-slot:name,单个插槽可以直接在组件上使用。插槽详解

父组件(下面props为自己定义的变量):

<template>
  <div>
    <hello v-slot:default="props">
      <template>
        <div v-for="(item,index) in props.people" :key="index" >
          {{item}}-{{item.name}}
        </div>
      </template>
    </hello>
  </div>
</template>

<script>
import hello from "@/components/test.vue";
export default {
  components: {
    hello: hello
  },
};
</script>
<style scoped>
</style>

子组件:

<template>
  <div>
      <span>i'm child</span>
      <template>
          <!-- 此处插槽的内容会被覆盖 -->
          <slot :people="people">{{people[0].name}}</slot> 
      </template>
  </div>
</template>

<script>
export default {
  data () {
      return {
          people:[
              {
                  name: '张三',
                  age: 20,
                  sex: '男'
              },
              {
                  name: '李四',
                  age: 20,
                  sex: '女'
              },
              {
                  name: '王二',
                  age: 20,
                  sex: '男'
              }
          ]
      }
  },
};
</script>
<style lang='scss' scoped>
</style>

效果:
在这里插入图片描述
 
 
 

非父子组件传值(也可以通过vuex共享数据)

公共实例文件bus.js,作为公共数控中央总线:

import Vue from 'vue'

export default new Vue()

第一个组件 first.vue:

<template>
  <div>
      <h1>firstPage</h1> 
  </div>
</template>

<script>
import bus from '../bus'
export default {
  data() {
    return {
        message: '我是firstPage的内容'
    };
  },
  beforeDestroy() {
      bus.$emit('textFromPage1',this.message);
  },
};
</script>
<style scoped>
</style>

第二个组件second.vue:

<template>
  <div>
    <h1>secondPage</h1> 
    <h4>{{mes}}</h4>
  </div>
</template>

<script>
import bus from "../bus";
export default {
  data() {
    return {
      mes: ""
    };
  },
  created() {
    bus.$on("textFromPage1", (val) => {
      this.mes = val;
    });
  }
};
</script>
<style scoped>
</style>

 
 
博文参考:
https://www.cnblogs.com/jin-zhe/p/9291071.html

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值