VUE3 学习笔记(八-1)中 EasyUI 组件的使用方法

3 篇文章 0 订阅
文章介绍了如何在Vue3中使用Accordion组件,包括查看官方文档理解组件功能,使用Props设置边框和初始化选中项,通过Events监听用户选择的面板,以及运用Methods进行程序控制选择面板。示例代码详细展示了如何操作和绑定事件。
摘要由CSDN通过智能技术生成

目录

一、首先看官方 Accordion 文档说明

二、如何使用 Props (属性) 

三、如何使用 Events(事件)

 四、如何使用Methods(方法)

1. 通过 ref 给Vue3中的标签添加引用

2. 在script setup lang="ts"中定义变量引用

3. 增加一个按键,用来点击实现程序切换panel

五、完整的演示程序代码


EasyUI 组件的使用是有规律的,不同的组件可能拥有Props(属性)、Events(事件)、Scoped Slots(插槽)、Methods(方法),不一定四个属性同时拥有,这需要根据官方文档来查看。

那么这些Props(属性)、Events(事件)、Scoped Slots(插槽)、Methods(方法)该如何使用呢?

我们就用 Accordion(手风琴 )组件为例来说明:

一、首先看官方 Accordion 文档说明

<Accordion style="height:250px">
  <AccordionPanel :title="'Title1'">
    <p>Content1</p>
  </AccordionPanel>
  <AccordionPanel :title="'Title2'">
    <p>Content2</p>
  </AccordionPanel>
  <AccordionPanel :title="'Title3'">
    <p>Content3</p>
  </AccordionPanel>
</Accordion>

Props

NameTypeDescriptionDefault
borderbooleanDefines if to show the border.true
multiplebooleanTrue to enable expanding multiple panels at one time.false
animtebooleanDefines if to show animation effect when expand or collapse panels.false
selectedIndexnumber,arrayThe initialized selected panel index.0

Events

NameParameterDescription
panelSelectpanelEmitted when a panel is selected.
panelUnselectpanelEmitted when a panel is unselected.

Methods

NameParametersReturnDescription
selectindexvoidSelect a specified panel.
unselectindexvoidUnselect a specified panel.
getPanelindexAccordionPanelGet a specified panel.
getPanelIndexpanelnumberGet the specified panel's index.
getSelectedIndexnonenumberGet the first selected panel's index.
getSelectedPanelnoneAccordionPanelGet the first selected panel.
getSelectedPanelsnoneAccordionPanel[]Get all the selected panels.

二、如何使用 Props 属性 

我们用 border 属性为例,默认 border 属性是 true,表示有边框,我们可以通过这个属性设置为false 去掉边框。在 <Accordion> 标签中增加::border="false"即可。

<Accordion style="height:250px" :border="false">

 再比如期望初始的时候就打开第二个Title,也就是Title2,根据文档,可以设置selectedIndex属性为1(第一个的索引为0)。:selectedIndex="1"

<Accordion style="height:250px" :selectedIndex="1">

注意:上述代码去掉了 :border="false" 所以边框又出现了!

三、如何使用 Events(事件)

官方文档有一个 panelSelect,有一个参数是panel,就是当前包含的面板。该事件表示用户选择了某个面板就会触发,参数就是当前选中的面板。

<Accordion 
        style="height:250px"  
        :selectedIndex="1"
        @panelSelect="do_panelSelect"
        >

事件处理函数,就是显示出来title

function do_panelSelect(panel: any){
       console.log(panel.title)   //打印出当前选择的panel的标题
    }

 四、如何使用Methods(方法)

关于使用方法,稍微有点麻烦,需要用到标签引用,然后才可以使用方法。假如我们需要使用select方法,这个方法有一个参数index,就是表示通过程序选择第几个panel,不用通过鼠标点击。例如我们select(2),就表示选择第三个panel。要实现程序调用,请按照如下步骤:

1. 通过 ref 给Vue3中的标签添加引用

 <Accordion ref="myAccordion" 
        style="height:250px"  
        :selectedIndex="1"
        @panelSelect="do_panelSelect"
        >

注意上面代码中的 ref="myAccordion"

2. 在script setup lang="ts"中定义变量引用

import {ref } from 'vue';
const myAccordion = ref();     

 注意变量名称必须和标签中的定义一致,本例是: myAccordion,需要使用ref()

 3. 增加一个按键,用来点击实现程序切换panel

<ButtonGroup selectionMode="single">
   <LinkButton iconCls="icon-add" :toggle="true" :selected="true" @click="ClickSelect">选择select(2)</LinkButton>
</ButtonGroup>

点击时间函数:

function ClickSelect(){
   myAccordion.value.select(2)
}

注意 myAccordion.value.select(2) 函数调用

五、完整的演示程序代码

<template>
    <Accordion ref="myAccordion" 
        style="height:250px"  
        :selectedIndex="1"
        @panelSelect="do_panelSelect"
        >
        <AccordionPanel :title="'Title1'">
            <p>Content1</p>
        </AccordionPanel>
        <AccordionPanel :title="'Title2'">
            <p>Content2</p>
        </AccordionPanel>
        <AccordionPanel :title="'Title3'">
            <p>Content3</p>
        </AccordionPanel>
    </Accordion>
    
    <ButtonGroup selectionMode="single">
        <LinkButton iconCls="icon-add" :toggle="true" :selected="true" @click="ClickSelect">选择select(2)</LinkButton>
    </ButtonGroup>
</template>
<script setup lang="ts">
    import {ref } from 'vue';
    const myAccordion = ref();                

    function do_panelSelect(panel: any){
       console.log(panel.title)   //打印出当前选择的panel的标题
    }

    function ClickSelect(){
        myAccordion.value.select(2)
    }
    
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海纳老吴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值