组件传值/动态组件/插槽

组件传值
父-》子
props
在data数据里定义
<子组件 :msg='message'></子组件>

data(){
return{
 message:'父组件中的值'
 }
}

子组件
<p>{{msg}}</p>
export default{
 props:['msg']
}

子-》父
子组件
<template>
  <div>
    <h4>count的值是:{{ count }}</h4>
    <button @click="fn">点击按钮+{{ num }}</button>
  </div>
</template>
export default {
 data() { 
    return {
      count: 0,
    };
  }
methods: {
    fn() {
      this.count +=1;
      this.$emit('numchange', this.count)
    },
  },
}
父组件
<子组件  @numchange="getNum"></子组件>
methods: {
    getNum(val) {
      console.log('接收的数据', val)
      this.const = val
    }
  },

兄弟-》兄弟
传值方
<template>
  <div>
    <h1>{{ count }}</h1>
    <button @click="add"></button>
  </div>
</template>
<script>
import bus from './eventBus.js'
export default {
  data() {
    return {
      count: 0
    }
  },
  methods: {
    add() {
      this.count += 1
      bus.$emit('fn', this.count)
    }
  }
}
接收方
<template>
  <div>
    <h1>{{ countLeft }}</h1>
  </div>
</template>
<script>
import bus from './eventBus.js'
export default {
  data() {
    return {
      countLeft: 0
    }
  },
  created() {
    bus.$on('fn', val => {
      this.countLeft = val
    })
  }
}
</script>
中间
eventBus.js
import Vue from 'vue'
export default new Vue()

ref
在不获取DOM API的前提下,能直接获取元素的引用
this.$nextTick(fn()=>{})延迟fn函数的执行,延迟到DOM元素更新完在执行fn函数,可以操作最新的DOM元素
<template>
  <div>
    <h1 ref="myRef">App根组件</h1>
    <button @click="changeColor"></button>
    <button @click="callAdd">点击按钮调用left组件add方法</button>
    <hr />
    <Left  ref="leftRef"></Left>
    <right></right>
  </div>
</template>

<script>
import Left from './components/Left.vue'
import Right from './components/Right.vue'
export default {
  data() {
    return {
    
    }
  },
  methods: {
    changeColor() {
      this.$refs.changeColor.style.color = 'red'
    },
    callAdd() {
      this.$refs.leftRef.add()
    }
  },
  components: {
    Left,
    Right
  }
}
</script>

动态组件
基于<component> 标签动态切换显示与隐藏
is属性表示要展示的组件名称 
keep-alive 可以缓存组件 防止组件销毁
include 指定要缓存那些组件多个以英文逗号分开
<keep-alive include="Left">
      <component is="Left"></component>
 </keep-alive>
只有被keep-alive控制时才会有
 deactivated() {
      console.log('组件被缓存了');
    },
    activated() {
      console.log('组件被激活了');
    },

插槽
提高组件的复用性  v-slot可简写#
定义插槽
<template>
  <div>
    <div>
      <!-- 具名插槽 -->
      <slot name="title">默认标题</slot>
      <!-- 默认插槽 -->
      <slot></slot>
    </div>
    <!-- 作用域插槽 -->
    <slot name="author" infor="WXQ"></slot>
  </div>
</template>
使用插槽
<template>
  <div class="left-container">
    <Article>
      <template v-slot:title>
        <h1 slot>ZY</h1>
      </template>
      <p>xxxxxxxxxxx</p>
      <p>xxxxxxxxxxx</p>
      <p>xxxxxxxxxxx</p>
      <p>xxxxxxxxxxx</p>
      <template v-slot:author="obj">
        <i>文章作者:{{ obj.infor }}</i>
      </template>
    </Article>
  </div>
</template>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值