vue-ts课程element-plus 动态组件icon

本文探讨了如何在Vue项目中全局注册element-plus的Icon组件,并通过Icons函数实现动态菜单,详细介绍了如何根据请求数据处理item.icon。

1.全局注册element-plus icon 组件

2.在menu中使用动态组件,以函数Icons(item)控制。

3.Icons(item)函数:函数作用为根据请求的数据将item.icon进行切割重组。

4.结果:

 

 

### 使用 Vue 3 和 TypeScript 创建带有 Element Plus 的凭证组件 #### 创建 Vite 项目并集成必要依赖项 为了实现一个完整的凭证组件,首先需要设置开发环境。这包括创建一个新的 Vite 项目,并安装必要的库。 ```bash npm create vite@latest my-vue-app --template vue-ts cd my-vue-app npm install ``` 接着安装所需的包: ```bash npm install element-plus axios pinia @types/axios typescript vue-router ``` #### 配置 `main.ts` 文件引入全局样式和插件 在项目的入口文件 `src/main.ts` 中初始化应用实例之前加载 Element Plus 及其他工具类库。 ```typescript import { createApp } from 'vue' import App from './App.vue' // 导入element plus 并注册为全局组件 import ElementPlus from 'element-plus'; import 'element-plus/dist/index.css'; const app = createApp(App) app.use(ElementPlus) app.mount('#app') ``` #### 构建登录表单组件 接下来定义具体的凭证输入框逻辑,在此案例中是一个简单的用户名密码验证界面。 ##### LoginInput.vue ```html <template> <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleFormRef"> <el-form-item label="Username" prop="username"> <el-input v-model="ruleForm.username"></el-input> </el-form-item> <el-form-item label="Password" prop="password"> <el-input type="password" autocomplete="off" show-password v-model="ruleForm.password"></el-input> </el-form-item> <el-button type="primary" @click.prevent="submitForm(ruleFormRef)">Submit</el-button> </el-form> </template> <script lang="ts"> import { defineComponent, reactive, toRefs, Ref, ref } from "vue"; import type { FormInstance } from 'element-plus' export default defineComponent({ name: "LoginInput", setup() { const ruleFormRef = ref<FormInstance>() interface RuleForm { username?: string; password?: string; } let state = reactive<RuleForm>({ username: '', password: '' }); const rules = reactive({ username: [ { required: true, message: 'Please input your Username', trigger: ['blur'] }, ], password: [ { required: true, message: 'Please enter the Password', trigger: ['change','blur']} ] }) function submitForm(formEl: FormInstance | undefined) { if (!formEl) return; formEl.validate((valid) => { if (valid){ console.log('submit!',state); } else{ console.error('error submit!'); return false; } }); }; return {...toRefs(state), rules, submitForm, ruleFormRef}; } }) </script> ``` 上述代码展示了如何利用 Vue 3 结合 TypeScript 来编写一个具备基本校验功能的登录表单项[^1]。通过这种方式可以很容易扩展成更复杂的认证流程或者与其他服务端API对接完成实际业务需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值