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一样,略写

总结

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

相关文章

  • vue2中Print.js的使用超详细讲解(pdf、html、json、image)

    vue2中Print.js的使用超详细讲解(pdf、html、json、image)

    项目中有用到打印功能,网上就找了print.js,下面这篇文章主要给大家介绍了关于vue2中Print.js使用(pdf、html、json、image)的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-03-03
  • Vue如何获取下拉框中选中的value值和label值

    Vue如何获取下拉框中选中的value值和label值

    这篇文章主要介绍了Vue如何获取下拉框中选中的value值和label值问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • vue element el-select下拉滚动加载的方法

    vue element el-select下拉滚动加载的方法

    很多朋友都遇到这样一个问题在使用vue+element的el-select下拉框加载返回数据太多时,会造成卡顿,用户体验欠佳,这篇文章主要介绍了vue element el-select下拉滚动加载方法,需要的朋友可以参考下
    2022-11-11
  • Vite创建Vue3项目及Vue3使用jsx详解

    Vite创建Vue3项目及Vue3使用jsx详解

    vite是新一代的前端构建工具,下面这篇文章主要给大家介绍了关于Vite创建Vue3项目以及Vue3使用jsx的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-08-08
  • vue如何动态设置class、动态设置style

    vue如何动态设置class、动态设置style

    这篇文章主要介绍了vue如何动态设置class、动态设置style,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue中怎样让函数只执行一次

    vue中怎样让函数只执行一次

    这篇文章主要介绍了vue中怎样让函数只执行一次问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • 使用 vue 实例更好的监听事件及vue实例的方法

    使用 vue 实例更好的监听事件及vue实例的方法

    这篇文章主要介绍了使用 vue 实例更好的监听事件及vue实例的方法,介绍了一种新增 vue 实例的方法,单独监听事件,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-04-04
  • Vue中常用的rules校验规则的实现

    Vue中常用的rules校验规则的实现

    在vue开发中,难免遇到各种表单校验,本文主要介绍了Vue中常用的rules校验规则的实现,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • 解决vue this.$forceUpdate() 处理页面刷新问题(v-for循环值刷新等)

    解决vue this.$forceUpdate() 处理页面刷新问题(v-for循环值刷新等)

    这篇文章主要介绍了解决vue this.$forceUpdate() 处理页面刷新问题(v-for循环值刷新等),解决方法是使用this.$forceUpdate()强制刷新,文章给大家分享了代码案例,需要的朋友参考下吧
    2018-07-07
  • Webpack打包图片-js-vue 案例解析

    Webpack打包图片-js-vue 案例解析

    在开发中我们会有各种各样的模块依赖,这些模块可能来自于自己编写的代码,也可能来自第三方库,本文给大家介绍Webpack打包图片-js-vue的相关知识,感兴趣的朋友跟随小编一起看看吧
    2023-11-11

最新评论