vite+vue3+element-plus搭建项目的踩坑记录

 更新时间:2023年10月09日 09:38:18   作者:周皮皮皮皮皮皮  
这篇文章主要介绍了vite+vue3+element-plus搭建项目的踩坑记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

需求:

因为vue3出了一段时间了,element也出了基于vue3.x版本的element-plus,vite打包听说很快,尝试一下。

一、用vite构建项目

npm install -g create-vite-app
create-vite-app vite_demo
npm install

二、安装element-plus依赖

在main.js全局引入

PS:

element-plus不兼容element-ui,一些提示类组件前面要加El,比如ElMessage

npm install element-plus

三、main.js

import { createApp } from 'vue'
import App from './App.vue'
import './index.css'
import ElementPlus from 'element-plus'
import 'element-plus/lib/theme-chalk/index.css'
import store from './store'
import router from './router'
import '/@/permission' // permission control
const app = createApp(App)
app
  .use(ElementPlus)
  .use(router)
  .use(store)
app.mount('#app')

四、引入vue-router

区别:

  • 引入和vue2.x有区别;
  • 引入界面要加后缀.vue

1. 安装4.0.3版本

npm install vue-router@4.0.3

2. router/index.js

// 与vue2.x的区别
// import Vue from 'vue'
// import Router from 'vue-router'
// Vue.use(Router)
import { createRouter, createWebHashHistory } from 'vue-router'
export const constantRoutes = [
  // 基础路由
  {
    path: '/login',
    component: () => import('/@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('/@/views/404.vue'),
    hidden: true
  }
]
const router = createRouter({
  routes: constantRoutes,
  history: createWebHashHistory()
})
export default router

五、引入Vuex

区别:

  • 引入和vue2.x的有区别

1. 安装vuex依赖

npm install vuex@4.0.0-rc.2

2. store/index.js

// 和Vue2.x的区别
// import Vue from 'vue'
// import Vuex from 'vuex'
// Vue.use(Vuex)
// const store = new Vuex.Store({})
import { createStore } from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'
import permission from './modules/permission'
const store = createStore({
  modules: {
    app,
    settings,
    user,
    permission
  },
  getters
})
export default store

六、配置vite.config.js、env.development

区别:

  • 1. 之前是用的vue.config.js,vite用的是vite.config.js,重点是注意路径别名@是/@/ 
  • 2. env.development配置前缀不一样

1. vite.config.js

const path = require('path')
console.log(path.resolve(__dirname, './src'))
module.exports = { 
  outDir: 'target',
  port: 3000,
  open: false, // 是否自动在浏览器打开
  https: false, // 是否开启 https
  ssr: false, // 服务端渲染
  optimizeDeps: {
    include: ['moment', 'echarts', 'axios', 'mockjs']
  },
  alias: {
    // 注意!键必须以斜线开始和结束
    '/@/': path.resolve(__dirname, './src')
  },
  proxy: {
    '/cims': 'http://XXX'
  }
}

2. env.development,前缀是VITE_

ENV = 'development'
# base api
VITE_APP_BASE_API = '/cims'

引入使用如下

import.meta.env.VITE_APP_BASE_API

七、登录模块view/login/index

<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="content-login">
        <el-form-item prop="username">
          <span class="svg-container">
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
        </el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          class="btn-login"
          @click.native.prevent="handleLogin"
        >登 录</el-button>
      </div>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  methods: {
    handleLogin() {
      if (
        this.loginForm.username !== '' &&
        this.loginForm.password !== '' &&
        this.loginForm.code !== ''
      ) {
        this.loading = true
        // const that = this
        this.$store
          .dispatch('user/login', this.loginForm)
          .then(user => {
            this.$router.push({ path: this.redirect || '/' })
            this.changePicCode()
            this.loading = false
          })
          .catch(() => {
            this.loginForm.keyId = this.guid()
            this.loading = false
          })
      } else {
        this.showErrorInfo('用户名、密码、验证码都必填')
      }
    }
  }
}
</script>
 

八、引入sass

区别:

  • package.json里面sass配置要写在devDependencies

九、其他像axios,permission等

都和vue2.x一样,略写

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue3界面使用router及使用watch监听router的改变

    vue3界面使用router及使用watch监听router的改变

    vue2中使用router非常简单,但是vue3中略微有些改变,通过本文讲解下他的改变,对vue3 watch监听router相关知识感兴趣的朋友一起看看吧
    2022-11-11
  • element表格行列的动态合并示例详解

    element表格行列的动态合并示例详解

    这篇文章主要为大家介绍了element表格行列的动态合并示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • vue祖孙组件之间的数据传递案例

    vue祖孙组件之间的数据传递案例

    这篇文章主要介绍了vue祖孙组件之间的数据传递案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • vue3+elementUI实现悬浮多行文本输入框效果

    vue3+elementUI实现悬浮多行文本输入框效果

    这篇文章主要为大家详细介绍了vue3如何引用elementUI实现悬浮文本输入框效果,以便实现多行文本输入,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-10-10
  • 基于 Vue.js 2.0 酷炫自适应背景视频登录页面实现方式

    基于 Vue.js 2.0 酷炫自适应背景视频登录页面实现方式

    本文讲述如何实现拥有酷炫背景视频的登录页面,浏览器窗口随意拉伸,背景视频及前景登录组件均能完美适配,背景视频可始终铺满窗口,前景组件始终居中,视频的内容始终得到最大限度的保留,可以得到最好的视觉效果
    2018-01-01
  • vue的url请求图片的问题及请求失败解决

    vue的url请求图片的问题及请求失败解决

    这篇文章主要介绍了vue的url请求图片的问题及请求失败解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • vue组件实现移动端九宫格转盘抽奖

    vue组件实现移动端九宫格转盘抽奖

    这篇文章主要为大家详细介绍了vue组件实现移动端九宫格转盘抽奖,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • vue3自定义指令自动获取节点的width和height代码示例

    vue3自定义指令自动获取节点的width和height代码示例

    这篇文章主要介绍了如何使用ResizeObserver监听组件的宽度和高度,并将其封装成一个指令以便全局或局部使用,ResizeObserver可以监听元素的多个尺寸属性,如top、bottom、left等,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-11-11
  • vue-cli 自定义指令directive 添加验证滑块示例

    vue-cli 自定义指令directive 添加验证滑块示例

    本篇文章主要介绍了vue-cli 自定义指令directive 添加验证滑块示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • 详解如何在Vue3中实现懒加载组件

    详解如何在Vue3中实现懒加载组件

    随着现代前端框架的发展,懒加载作为一种优秀的性能优化技术,在用户体验和加载速度上扮演着越来越重要的角色,本文将详细介绍如何在 Vue 3 中实现懒加载组件,确保你能够将这一技术应用到自己的项目中,需要的朋友可以参考下
    2024-11-11

最新评论