@neosjs/textellipsis
文本省略组件。超出指定行数时自动截断并显示省略号,支持头部/中间/尾部三种省略位置,以及"展开 / 收起"操作。窗口宽度变化时自动重新计算。
安装
sh
pnpm add @neosjs/textellipsissh
npm i @neosjs/textellipsissh
yarn add @neosjs/textellipsis快速上手
通过 content 传入文本,rows 指定最大行数,超出部分自动省略。
vue
<script setup lang="ts">
import { TextEllipsis } from '@neosjs/textellipsis'
</script>
<template>
<TextEllipsis
content="这是一段很长的文本,超过一行时会被自动省略显示。"
:rows="1"
/>
</template>展开与收起
设置 expand-text 后,发生省略时会显示操作文字,点击可展开/收起全文;也可以通过 action 插槽自定义操作内容。
vue
<script setup lang="ts">
import { TextEllipsis } from '@neosjs/textellipsis'
import { ref } from 'vue'
const text = ref('很长的正文内容……很长的正文内容……很长的正文内容……')
</script>
<template>
<TextEllipsis
:content="text"
:rows="2"
expand-text="展开"
collapse-text="收起"
/>
<TextEllipsis :content="text" :rows="1" position="middle">
<template #action="{ expanded }">
<span class="link">{{ expanded ? '收起' : '展开' }}</span>
</template>
</TextEllipsis>
</template>Props
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| rows | number | string | 1 | 最大展示行数 |
| dots | string | '...' | 省略占位符号 |
| content | string | '' | 文本内容 |
| expand-text | string | '' | 展开操作的文字,为空则不显示 |
| collapse-text | string | '' | 收起操作的文字 |
| position | 'start' | 'middle' | 'end' | 'end' | 省略位置 |
Emits
| 事件名 | 参数 | 说明 |
|---|---|---|
| clickAction | (event: MouseEvent) | 点击展开/收起操作时触发 |
Slots
| 插槽名 | 参数 | 说明 |
|---|---|---|
| action | expanded: boolean | 自定义展开/收起操作内容,参数为当前是否展开 |
方法
通过 ref 可调用以下方法:
| 方法名 | 参数 | 说明 |
|---|---|---|
| toggle | (expanded?: boolean) | 切换展开 / 收起,可传入目标状态 |