当前位置: 首页 > news >正文

从 0 搭建 Vite 3 + Vue 3 Js版 前端工程化项目

之前分享过一篇vue3+ts+vite构建工程化项目的文章,针对小的开发团队追求开发速度,不想使用ts想继续使用js,所以就记录一下从0搭建一个vite+vue3+js的前端项目,做记录分享。

技术栈

  • Vite 3 - 构建工具

  • Vue 3

  • Vue Router - 官方路由管理器

  • Pinia -  Vue Store你也可以选择vuex

  • element-plus - UI组件库

  • Sass - CSS 预处理器

  • Axios - 一个基于 promise 的网络请求库,可以用于浏览器和 node.js

  • Vscode - 一个还挺好用的开发工具

一、项目的基础搭建

1、构建有多种方式,本次示例选择vue 官方脚手架工具create-vue 构建的

(1)使用vite的方式,构建一个基础模板

# npm 6.x
npm create vite@latest my-vue-app --template vue# npm 7+, extra double-dash is needed:
npm create vite@latest my-vue-app -- --template vue# yarn
yarn create vite my-vue-app --template vue# pnpm
pnpm create vite my-vue-app --template vue

 (2)使用vue官方脚手架,本教程使用此方式

npm init vue@latest

 二、安装UI组件库

安装css预处理器sass

npm install -D sass
或
npm add -D sass
1、在项目中安装element-plus
# NPM
$ npm install element-plus --save# Yarn
$ yarn add element-plus# pnpm
$ pnpm install element-plus
2、在mian.ts中引入并配置element-plus(完整引入)element-plus连接
import { createApp } from "vue";
import { createPinia } from "pinia";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import App from "./App.vue";
import router from "./router";import "./assets/main.css";const app = createApp(App);app.use(createPinia());
app.use(router);app.use(ElementPlus);app.mount("#app");
 3、按需导入,自动导入(推荐,此教程使用此方法)以下摘自element

首先你需要安装unplugin-vue-components 和 unplugin-auto-import这两款插件

npm install -D unplugin-vue-components unplugin-auto-import

然后把下列代码插入到你的 Vite 或 Webpack 的配置文件中

Vite
// vite.config.ts
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'export default defineConfig({// ...plugins: [// ...AutoImport({resolvers: [ElementPlusResolver()],}),Components({resolvers: [ElementPlusResolver()],}),],
})
4、在组件中使用
<template><div class="about"><h1>This is an about page</h1><el-button type="primary" @click="alertFun">Primary</el-button><el-button type="success">Success</el-button></div><el-dialog v-model="dialogVisible" title="Tips" width="30%" :before-close="handleClose"><span>This is a message</span><template #footer><span class="dialog-footer"><el-button @click="dialogVisible = false">Cancel</el-button><el-button type="primary" @click="dialogVisible = false">Confirm</el-button></span></template></el-dialog>
</template>
<script setup>
let dialogVisible = ref(false)
const handleClose = () => {ElMessageBox.confirm('Are you sure to close this dialog?').then(() => {done()}).catch(() => {// catch error})
}
const alertFun = () => {ElMessage({message: 'Congrats, this is a success message.',type: 'success',})dialogVisible.value = true
}
</script>
<style>
@media (min-width: 1024px) {.about {min-height: 100vh;display: flex;align-items: center;}
}
</style>

效果:

 5、全局配置elementPlus  size 和 zIndex 语言等
<template><!-- 汉化 element --><el-config-provider :locale="zhCn" :size="elSize" :z-index="zIndex"><RouterView /></el-config-provider>
</template><script setup>
import zhCn from "element-plus/es/locale/lang/zh-cn";
const zIndex = 3000, elSize = 'default';</script>
<style scoped></style>

三、安装自动导入插件

http://www.lryc.cn/news/247203.html

相关文章:

  • 【论文阅读笔记】Smil: Multimodal learning with severely missing modality
  • 在Windows系统上安装git-Git的过程记录
  • qt QString常用方法
  • 吴恩达《机器学习》10-6-10-7:学习曲线、决定下一步做什么
  • 分子骨架跃迁工具-DiffHopp 评测
  • MySQL双主双从数据库集群搭建
  • vue实现动态路由菜单!!!
  • 企业如何选择安全又快速的大文件传输平台
  • springboot 自定义starter逐级抽取
  • GAN:ImprovedGAN-训练GAN的改进策略
  • docker限制容器内存的方法
  • 阿里达摩院裁撤量子实验室
  • mysql数据库基础知识,Mysql的索引和主键区别,数据库的事务的基本特性
  • 解决Vscode使用git提交卡住的问题
  • Linux C语言 32-网络编程之UDP例程
  • ubuntu22.04系统下载程序和依赖,并拷贝到指定路径下
  • Kafka KRaft 版本集群部署详细教程(附配置文件详细解释)
  • 在龙蜥 anolis os 23 上 源码安装 PostgreSQL 16.1
  • UDP的不可靠性可以用来做什么
  • vue3还用this吗?getCurrentInstance获取当前组件实例
  • 高校学生宿舍公寓报修维修生活管理系统 微信小程序b2529
  • C++类与对象(7)—友元、内部类、匿名对象、拷贝对象时编译器优化
  • Django回顾2
  • <JavaDS> 二叉树遍历各种遍历方式的代码实现 -- 前序、中序、后序、层序遍历
  • 如何控制Spring工厂创建对象的次数?详解Spring对象的声明周期!
  • 计算机杂谈系列精讲100篇-【计算机应用】PyTorch部署及分布式训练
  • Opencv-C++笔记 (19) : 分水岭图像分割
  • Linux以nohup方式运行jar包
  • 【c++|SDL】开始使用之---demo
  • leetcode:有效的括号