Vue学习(组件传参)-学习笔记

本文详细介绍了Vue中父组件向子组件传递数据(如对象、字符串、事件)的方法,包括Props、ref以及兄弟组件间的通信技巧。重点讲解了父子组件之间的交互,如`$emit`和`$on`,以及如何通过`ref`操作DOM和子组件的方法。
摘要由CSDN通过智能技术生成

Vue学习(组件传参)-学习笔记

父到子

Father:(index)

<template>
<div>
  <Banner :img="img" />

  <table class="table"> 
    <thead>
      <tr >
        <th>用户姓名</th>
        <th>用户性别</th>
        <th>所在城市</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(v,i) in lists" :key="i">
          <td>{{v.name}}</td>
          <td>{{v.sex}}</td>
          <td>{{v.city}}</td>
          <td>
            <!-- 传入不同的标题 -->
            <!-- <Btn :title="'修改'" />
            <Btn :title="'删除'" />
            <Btn :title="'详情'" /> -->
            <!-- 传入多个属性 -->
            <!-- <Btn :title="'详情'" :color="'red'" />
            <Btn :title="'删除'" :color="'blue'" /> -->
            <!-- 传入对象 -->
            <Btn :obj = "obj" />
            <Btn :obj = "obj2" />
          </td>
      </tr>
    </tbody>
  </table>
</div>
</template>

<script>
import Btn from './Btn.vue'
import Banner from './Banner.vue'

export default {
  name:'',
  data(){
    return{
      title:'标题',
      obj:{title:'修改',color:'green'},
      obj2:{title:'删除',color:'red'},
      lists:[
          {name:'张三1',sex:'男',city:'北京1',check:true},
          {name:'张三2',sex:'女',city:'北京2',check:false},
          {name:'张三3',sex:'男',city:'北京3',check:false},
          {name:'张三4',sex:'男',city:'北京4',check:false},
          {name:'张三5',sex:'女',city:'北京5',check:false},
          {name:'张三6',sex:'男',city:'北京6',check:false},
          {name:'张三7',sex:'男',city:'北京7',check:true},
      ],
      img:[
        require("@/assets/img/1.jpg"),
        require("@/assets/img/2.jpg"),
        require("@/assets/img/1.jpg"),
        require("@/assets/img/2.jpg")]
    }
  },
  components:{
    Btn,
    Banner
  }
}
</script>

<style scoped>
ul {
    list-style-type:none;
}
.table{
    width: 70%;
    border-collapse: collapse;
    margin: 0 auto;
    text-align: center;
}
.table td,
.table th{
    border: 1px solid #ddd;
    padding: 10px;
}
.table thead tr {
	background:#1f76b3;
	color:#fff;
} 
</style>

Son1:(Btn)

<template>
  <button :style="{color:obj.color}">{{obj.title}}</button>
</template>

<script>
export default {
  name:'',
  data(){
    return{

    }
  },
  //父到子传递参数
  //props:['title','color','obj','event'],  //字符串数组写法
  props:{    //对象写法
    //title:String,
    //color:String,
    // title:{
    //   type:String,
    //   default:'标题'
    // }
    obj:{
      title:String,
      color:String
    }
  },
  methods:{
      
  }
}
</script>

<style scoped>

</style>

Son2:(Banner)(可将banner作为公共的)

<template>
  <div class="banner">
      <img v-for="(v,i) in img" :key="i" :src="v" v-show="n==i"/>
      <!-- <img src="@/assets/img/1.jpg"/>  -->
      <div class="banner-circle">
          <ul>
              <li  v-for="(v,i) in img" :key="i" :class="n==i?'selected':''"></li>  
          </ul> 
      </div>
  </div>
</template>

<script>
export default {
  name:'',
   props:{    //对象写法
    img:{

    }
  },
  data(){
    return{
      n:0,
      timer:null, //定时器
      // img:[
      //   require("@/assets/img/1.jpg"),
      //   require("@/assets/img/2.jpg")]
    }
  },
  methods:{
    autoPlay(){
      this.timer = setInterval(this.play,2000);
    },
    play(){
      this.n++;
      if(this.n>=this.img.length){
        this.n = 0;
      }
    }
  },
  mounted(){   //挂载完成
    this.autoPlay();
  },
  destroyed(){  //销毁
    clearInterval(this.timer)
  }
}
</script>

子到父

Son:(Btn)

<template>
  <button @click="send()">按钮</button>
</template>

<script>
export default {
  name:'',
  data(){
    return{
      msg:'我是按钮组件'
    }
  },
  props:{    
    event:Function
  },
  methods:{
      send(){
        //this.$emit('e-child',this.msg);   //发射
         this.$emit('e-child',this.event);   //发射
      }
  }
}
</script>

Father:(index)

<template>
<div>
  {{title}}
  <table class="table"> 
    <thead>
      <tr >
        <th>用户姓名</th>
        <th>用户性别</th>
        <th>所在城市</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(v,i) in lists" :key="i">
          <td>{{v.name}}</td>
          <td>{{v.sex}}</td>
          <td>{{v.city}}</td>
          <td>
            <!-- 子传父  接收消息@e-child="acceptSon-->
            <Btn  @e-child="acceptSon"/>
            <!-- 子操作父的方法  :event="add" 传入父的方法-->  //就是把event传过去给子,子接收后再通过发射传回来父操作
            <Btn :event="add"  @e-child="acceptSon"/>
          </td>
      </tr>
    </tbody>
  </table>
</div>
  
</template>

<script>
import Btn from './Btn.vue'

export default {
  name:'',
  data(){
    return{
      lists:[
          {name:'张三1',sex:'男',city:'北京1',check:true},
          {name:'张三2',sex:'女',city:'北京2',check:false},
          {name:'张三3',sex:'男',city:'北京3',check:false},
          {name:'张三4',sex:'男',city:'北京4',check:false},
          {name:'张三5',sex:'女',city:'北京5',check:false},
          {name:'张三6',sex:'男',city:'北京6',check:false},
          {name:'张三7',sex:'男',city:'北京7',check:true},
      ]
    }
  },
  methods:{
    add(){
      this.title = 10;
    },
     acceptSon(d){  //接收子消息
      //this.title = d;  //d=msg
      d();
     }
  },
  components:{
    Btn
  }
}
</script>

<style scoped>
ul {
    list-style-type:none;
}
.table{
    width: 70%;
    border-collapse: collapse;
    margin: 0 auto;
    text-align: center;
}
.table td,
.table th{
    border: 1px solid #ddd;
    padding: 10px;
}
.table thead tr {
	background:#1f76b3;
	color:#fff;
} 
</style>

父操作子-ref(类似于操作dom)

父(index)

<template>
<div>
  姓名:<input type='text' ref='input'/>
  <h1 ref='h1'>{{title}}</h1>
  <button @click="change()">按钮</button>
  <div>
    子组件:<Btn :obj="obj" ref='btn'/>
  </div>
  
</div>
  
</template>

<script>
import Btn from './Btn.vue'

export default {
  name:'',
  data(){
    return{
      title:'标题',
      obj:{title:"按钮",color:"red"}
 
    }
  },
  methods:{
    change(){
     // console.log(this.$refs.btn);
     // this.title= 12432432;
      this.$refs.input.focus();
      this.$refs.h1.style.color='red';
      //this.title = this.$refs.btn.msg;//直接获取子组件中的属性
      this.title = this.$refs.btn.send(); //直接操作子组件中的方法
    }
  },
  components:{
    Btn
  }
}
</script>

子(Btn)

<template>
  <button :style="{color:obj.color}" >{{obj.title}}</button>
</template>

<script>
export default {
  name:'',
  data(){
    return{
      msg:'我是按钮组件'
    }
  },
  //父到子传递参数
  //props:['title','color'],  //字符串数组写法
  props:{    //对象写法
    //title:String,
    //color:String,
    // title:{
    //   type:String,
    //   default:'标题'
    // }
    obj:{
      title:String,
      color:String
    },
    event:Function
  },
  methods:{
      send(){
        return 100
      }
  }
}
</script>

兄弟之间传参

父(index)- 中间桥梁作用

<template>
<div>
  <h1>父组件:</h1>
  <!-- 第一种:testB发射到父,父改变title值 -->
  <!-- <testA :title="title"/> -->
  <!-- <testB @e-child="acceptSon"/> -->
  <!-- 第二种  ref 只能获取到B的值,不能修改-->
  <testB ref='testB' />
  <testA :title="title"/>
</div>
</template>

<script>
import testA from './testA.vue'
import testB from './testB.vue'

export default {
  name:'',
  data(){
    return{
      title:'123'
    }
  },
  mounted(){  //考虑到加载顺序,在testA取值时,testB还没有加载完,在等挂载完成后再赋值
    this.title = this.$refs.testB.title;
  },
  methods:{
     acceptSon(res){
       this.title = res;
     }
  },
  components:{
    testA,
    testB
  }
}
</script>

兄(testA)

<template>
<div>
  testA组件:{{title}}  
</div>  
</template>

<script>
export default {
  name:'',
  data(){
    return{
      //title:'testA'
    }
  },
  props:{  //父传子
    title:String
  }
}
</script>

弟(testB)

<template>
<div>
  testB组件:
  <input type="text" v-model="title" @input="send()"/>
</div> 
</template>

<script>
export default {
  name:'',
  data(){
    return{
      title:'testB'
    }
  },
  methods:{
     send(){
       this.$emit('e-child',this.title);
     }
  }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值