组件相关知识

本文介绍了Vue.js中组件的基本使用,包括导入、注册和使用组件。详细讲解了如何通过props实现父传子数据传递,以及如何利用$emit实现子传父事件通信。此外,还探讨了组件设计的原则,如设置props属性、数据、方法和事件,以及如何设计一个SwiperCom组件,用于实现轮播图功能,包括自动播放、边界检查等。
摘要由CSDN通过智能技术生成

一、 定义和使用:

例:
CounterCom.vue

<template>
<button>1</button>
</template>

  App.vue

01 导入

import CounterCom from './components/CounterCom.vue'

02注册

components:{{CounterCom}}

03使用

<CounterCom></CounterCom>
<counter-com></counter-com>

二、 父传子

使用props
父传给子的数组是只读的(默认值,读取显示)
不能进行修改

 App.vue文件中

<CounterCom :num="10>

 Countercom.vue组件中

01 接收参数并 定义传递过来的参数是数字列席,默认值是1

props :{
"num":{type:Number,default:1}
}

02 使用参数num
 

data(){
return {counter:this.num}
}

 三、子传父

使用的事件 $emit

 CounterCom.vue

<button class="active" @click="counter++; $emit('counterchange',counter)">

App.vue

<CounterCom  @counterchange="n=$event"></CounterCom>
  •  $emit (事件名,事件值)  发送一次事件,事件名{counterchange}和事件值{counter}是自定义的
  •  $event固定写法 ,事件的值(counterchange 事件的值,也是子组件$emit的第二个参数)

 四、组件传参

子传父

父传子


五、组件的设计

以下面图片为例:

props

  • 默认值:value
  • 最大值:max
  • 最小值:min
  • 步进制:step(一次加多少)

data

 被改变的值 :num

methods

加add
减 minus     

 事件

 numchange  数字发生的变化

 template

   <span></span>
    <input type="">
    <button></button>

六、组件设计SwiperCom 

props

  •  gallery 传入图片的数组
  •  inter 间隔时间
  •  current 当前第几张幻灯片

data

   index 当前显示第几张

 methods

auto 自动播放

auto(){
		this.stopID=setInterval(()=>{
		// 每隔2500毫秒让index值加1
		// 检查index的边界
		this.index++;
		this.check();
		},2500)
},

check 检查边界

check(){
		// 如果index=gallery的长度 让index值=0
		if(this.index>=this.gallery.length){
		this.index=0
		}
}

overHd  输入移除

outHd   鼠标移除

 template

<div class="swiper">
    <div class="swiperitem">幻灯片<img></div>
    <div class="controls">上一张,下一张</div>
    <div class="thumbs">小圆点</div>
</div>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值