66
77在成功 [ 安装] ( ./installation.md ) Element-UI-X 之后,你可以开始使用组件库中的组件。以下是一个简单的示例,展示如何使用打字机组件:
88
9+ ::: demo
10+
911``` html
1012<template >
1113 <div class =" demo-container" >
2123 <div class =" avatar" >AI</div >
2224 <div class =" content" >
2325 <el-x-typewriter
24- :text =" aiResponse"
25- :type-speed = " 30 "
26- @typing-complete =" onTypingComplete"
26+ :content =" aiResponse"
27+ :typing = " { interval: 30, step: 1 } "
28+ @finish =" onTypingComplete"
2729 ref =" typewriter"
2830 />
2931 </div >
5254 mounted () {
5355 // 页面加载后自动开始打字效果
5456 this .$nextTick (() => {
55- this .$refs .typewriter .startTyping ();
57+ this .$refs .typewriter .restart ();
5658 });
5759 },
5860 methods: {
59- onTypingComplete (text ) {
60- console .log (' 打字效果完成:' , text );
61+ onTypingComplete (instance ) {
62+ console .log (' 打字效果完成:' , instance );
6163 },
6264 regenerateResponse () {
6365 // 模拟重新生成回复
64- this .$refs .typewriter .eraseAll ();
65- setTimeout (() => {
66- this .$refs .typewriter .startTyping ();
67- }, 300 );
66+ this .$refs .typewriter .restart ();
6867 },
6968 },
7069 };
131130 </style >
132131```
133132
133+ :::
134+
134135## 使用多个组件
135136
136137Element-UI-X 提供了多个组件,你可以组合使用它们来构建完整的 AI 交互界面。以下是一个简化的示例:
137138
139+ ::: demo
140+
138141``` html
139142<template >
140143 <div class =" ai-chat" >
141144 <!-- 思考动画组件 -->
142- <el-x-thinking v-if =" isThinking" />
145+ <el-x-thinking
146+ v-if =" isThinking"
147+ status =" thinking"
148+ content =" AI正在思考中..."
149+ />
143150
144151 <!-- 打字机组件 -->
145152 <el-x-typewriter
146153 v-else-if =" aiResponse"
147- :text =" aiResponse"
154+ :content =" aiResponse"
155+ :typing =" { interval: 40, step: 1 }"
148156 ref =" typewriter"
149157 />
150-
158+ < br />
151159 <!-- 发送框组件 -->
152160 <el-x-sender
153161 v-model =" userInput"
154- @send =" sendMessage"
162+ @submit =" sendMessage"
155163 :disabled =" isThinking"
164+ :loading =" senderLoading"
165+ ref =" sender"
166+ clearable
167+ @cancel =" handleCancel"
156168 />
157169 </div >
158170</template >
@@ -164,71 +176,78 @@ Element-UI-X 提供了多个组件,你可以组合使用它们来构建完整
164176 userInput: ' ' ,
165177 aiResponse: ' ' ,
166178 isThinking: false ,
179+ senderLoading: false ,
180+ timeoutId: null ,
167181 };
168182 },
169183 methods: {
170- sendMessage () {
171- if (! this . userInput .trim ()) return ;
184+ sendMessage (message ) {
185+ if (! message .trim ()) return ;
172186
173- const userMessage = this .userInput ;
174- this .userInput = ' ' ;
175- this .isThinking = true ;
187+ const userMessage = message;
188+ this .senderLoading = true ;
176189
177190 // 模拟AI响应
178- setTimeout (() => {
179- this .isThinking = false ;
180- this .aiResponse = ` 你发送的消息是: "${ userMessage} "。这是一个AI响应示例。` ;
181-
182- this .$nextTick (() => {
183- this .$refs .typewriter .startTyping ();
184- });
185- }, 1500 );
191+ this .timeoutId = setTimeout (() => {
192+ this .isThinking = true ;
193+ this .senderLoading = false ;
194+
195+ setTimeout (() => {
196+ this .isThinking = false ;
197+ this .aiResponse = ` 你发送的消息是: "${ userMessage} "。这是一个AI响应示例。` ;
198+
199+ this .$nextTick (() => {
200+ this .$refs .typewriter .restart ();
201+ });
202+ }, 1500 );
203+ }, 500 );
204+ },
205+ handleCancel () {
206+ this .senderLoading = false ;
207+ if (this .timeoutId ) {
208+ clearTimeout (this .timeoutId );
209+ this .timeoutId = null ;
210+ }
211+ this .$message .info (' 取消发送' );
186212 },
187213 },
188214 };
189215 </script >
190- ```
191-
192- ## 主题定制
193216
194- Element-UI-X 的组件样式继承自 Element UI 的主题系统,你可以通过修改主题变量来定制组件的外观:
195-
196- ``` scss
197- /* 在你的样式文件中 */
198- @import ' ~element-ui/packages/theme-chalk/src/common/var' ;
199-
200- /* 自定义 Element UI 主题变量 */
201- $--color-primary : #6b46c1 ;
202- $--color-success : #38a169 ;
203- $--color-warning : #d69e2e ;
204- $--color-danger : #e53e3e ;
205- $--color-info : #4299e1 ;
206-
207- /* 自定义 Element-UI-X 主题变量 */
208- $--color-ai-bubble-user : lighten ($--color-primary , 40% );
209- $--color-ai-bubble-bot : lighten ($--color-info , 40% );
210- $--color-ai-cursor : $--color-primary ;
211-
212- /* 引入组件样式 */
213- @import ' ~element-ui/packages/theme-chalk/src/index' ;
214- @import ' ~@element-x/core/src/theme/index' ;
217+ <style >
218+ .ai-chat {
219+ max-width : 800px ;
220+ margin : 0 auto ;
221+ padding : 20px ;
222+ border : 1px solid #ebeef5 ;
223+ border-radius : 4px ;
224+ }
225+ </style >
215226```
216227
228+ :::
229+
217230## 组件引用和方法调用
218231
219232Element-UI-X 的组件支持通过 ref 引用来调用组件方法:
220233
234+ ::: demo
235+
221236``` html
222237<template >
223238 <div >
224239 <el-x-typewriter
225240 ref =" typewriter"
226- :text =" text"
241+ :content =" text"
242+ :typing =" { interval: 40, step: 1 }"
227243 />
228244
229- <el-button @click =" startTyping" >开始打字</el-button >
230- <el-button @click =" typeAll" >立即完成</el-button >
231- <el-button @click =" eraseAll" >清空</el-button >
245+ <div class =" demo-controls" >
246+ <el-button-group >
247+ <el-button @click =" finishTyping" >立即完成</el-button >
248+ <el-button @click =" restart" >重新开始</el-button >
249+ </el-button-group >
250+ </div >
232251 </div >
233252</template >
234253
@@ -240,24 +259,29 @@ Element-UI-X 的组件支持通过 ref 引用来调用组件方法:
240259 };
241260 },
242261 methods: {
243- startTyping () {
244- this .$refs .typewriter .startTyping ();
245- },
246- typeAll () {
247- this .$refs .typewriter .typeAll ();
262+ finishTyping () {
263+ this .$refs .typewriter .finishTyping ();
248264 },
249- eraseAll () {
250- this .$refs .typewriter .eraseAll ();
265+ restart () {
266+ this .$refs .typewriter .restart ();
251267 },
252268 },
253269 };
254270 </script >
271+
272+ <style >
273+ .demo-controls {
274+ margin-top : 15px ;
275+ }
276+ </style >
255277```
256278
279+ :::
280+
257281## 下一步
258282
259283现在你已经了解了 Element-UI-X 的基本用法,可以:
260284
261285- 查看 [ 组件] ( ../components/ ) 文档了解每个组件的详细用法
262- - 查看 [ 示例] ( ../examples/ ) 了解更多实际应用场景
263- - 参考 [ 主题] ( ./theme.md ) 文档学习如何定制组件样式
286+ <!-- - 查看 [示例](../examples/) 了解更多实际应用场景
287+ - 参考 [主题](./theme.md) 文档学习如何定制组件样式 -->
0 commit comments