Vue项目 elementUI 按需导入

本文展示了如何利用Element UI库和Babel配置一个登录页面。代码示例中,详细地引入了组件、样式,并实现了输入验证、登录功能及密码显示切换。在`babel.config.js`文件中配置了Babel插件,而在`Login.vue`组件中,整合了Element UI的按钮和图标组件,以及登录方法和密码可见性切换。
摘要由CSDN通过智能技术生成

https://element.eleme.cn/#/zh-CN/component/quickstart

yarn add babel-plugin-component

babel.config.js:

module.exports = {
  presets: ['@vue/cli-plugin-babel/preset', ['@babel/preset-env', { modules: false }]],
  plugins: [
    [
      'component',
      {
        libraryName: 'element-ui',
        styleLibraryName: 'theme-chalk',
      },
    ],
  ],
}

Login.vue:

<template>
  <div>
    <div>
      <input v-model="username" placeholder="请输入用户名" autofocus />
    </div>
    <div>
      <input
        v-model="password"
        placeholder="请输入密码"
        :type="visible ? 'text' : 'password'"
        @keyup.enter="handleLogin"
      />
      <Icon
        :name="visible ? 'show' : 'close'"
        class="m-login-icon"
        @onClick="handleVisible"
      ></Icon>
    </div>
    <el-button type="primary" @click="handleLogin">登录</el-button>
  </div>
</template>

<script>
import { Icon } from '../../../components/light'
import Api from '../../../api'
import { Button } from 'element-ui'

export default {
  data() {
    return {
      username: 'admin',
      password: '123456',
      visible: false,
    }
  },
  components: {
    Icon,
    [Button.name]: Button
  },
  methods: {
    handleLogin() {
      Api.light
        .login({ username: this.username, password: this.password })
        .then((res) => {
          if (res.state === 1) {
            localStorage.setItem('token', res.data.username)
            this.$router.push('/light/index/home')
          }
        })
    },
    handleVisible() {
      this.visible = !this.visible
    },
  },
}
</script>

<style></style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

徐同保

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

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

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

打赏作者

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

抵扣说明:

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

余额充值