当前位置:   article > 正文

vite+vue3+typeScript+elementPlus(elementUI)搭建项目_使用vue3 + elementplus 快速搭建项目

使用vue3 + elementplus 快速搭建项目

使用vite+vue3创建项目

npm init vite@latest myVue //myVue为项目名称

按提示选择vue和ts,如下图

按提示切换到项目目录、添加依赖并运行项目

  1. cd myVue
  2. npm i
  3. run run dev

安装路由vue-router

npm i vue-router 

在src下添加路由文件router,如下图所示

路由设置代码如下

  1. import { createRouter, createWebHashHistory } from 'vue-router';
  2. const routes = [
  3. {
  4. path: "/",
  5. name: "Login",
  6. component: () => import("../views/login/index.vue"), //动态加载组件
  7. },
  8. {
  9. path: "/home",
  10. name: "Home",
  11. component: () => import("../views/home/index.vue")
  12. }
  13. ];
  14. //设置白名单免登录页面
  15. const whiteList = [
  16. { name: 'Login' }
  17. ]
  18. const router = createRouter({
  19. history: createWebHashHistory(),
  20. routes,
  21. });
  22. //路由守卫
  23. let isLogin:Boolean = true //模拟登录状态,一般判定是否存在token
  24. router.beforeEach(async (to, from, next) => {
  25. if (
  26. // 检查用户是否已登录
  27. !isLogin &&
  28. // 判断是否免登录路由
  29. whiteList.some(item => item.name== to.name)
  30. ) {
  31. // 将用户重定向到登录页面
  32. next({ name: 'Login' })
  33. } else next()
  34. })
  35. export default router

main.ts中添加路由

  1. import { createApp } from 'vue'
  2. import './style.css'
  3. import App from './App.vue'
  4. import router from './router/routes' //引入路由
  5. createApp(App).use(router).mount('#app') //设置使用路由

然后在app.vue中使用router-view添加路由展示

  1. <script setup lang="ts">
  2. </script>
  3. <template>
  4. <router-view></router-view>
  5. </template>
  6. <style scoped>
  7. </style>

引入element-plus

npm install element-plus --save

使用按需引用中的自动导入需要安装额外的三个插件:unplugin-vue-componentsunplugin-auto-import和unplugin-element-plus

npm i unplugin-vue-components unplugin-auto-import unplugin-element-plus

注:安装unplugin-element-plus前需要添加sass和sass-loader依赖

  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import ElementPlus from 'unplugin-element-plus/vite'
  4. import AutoImport from 'unplugin-auto-import/vite'
  5. import Components from 'unplugin-vue-components/vite'
  6. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  7. // https://vitejs.dev/config/
  8. export default defineConfig({
  9. plugins: [
  10. vue(),
  11. ElementPlus({
  12. useSource: true
  13. }),
  14. AutoImport({
  15. resolvers: [ElementPlusResolver()],
  16. }),
  17. Components({
  18. resolvers: [ElementPlusResolver()],
  19. })
  20. ]
  21. })

注:注意版本问题,本人开始安装的element-plus版本为1.x引用样式报错,测试可行配置如下图:

package.js代码如下:

  1. {
  2. "name": "v3.0",
  3. "private": true,
  4. "version": "0.0.0",
  5. "type": "module",
  6. "scripts": {
  7. "dev": "vite",
  8. "build": "vue-tsc && vite build",
  9. "preview": "vite preview"
  10. },
  11. "dependencies": {
  12. "element-plus": "^2.2.36",
  13. "sass": "^1.59.2",
  14. "vue": "^3.2.45",
  15. "vue-router": "^4.1.6"
  16. },
  17. "devDependencies": {
  18. "@types/node": "^18.15.0",
  19. "@vitejs/plugin-vue": "^4.0.0",
  20. "typescript": "^4.9.3",
  21. "unplugin-auto-import": "^0.15.1",
  22. "unplugin-element-plus": "^0.7.0",
  23. "unplugin-vue-components": "^0.24.1",
  24. "vite": "^4.1.0",
  25. "vite-plugin-style-import": "^0.10.1",
  26. "vue-tsc": "^1.0.24"
  27. }
  28. }

页面、组件内按需使用组件,页面代码如下:

  1. <script setup lang="ts">
  2. import router from '../../router/routes'
  3. import { reactive } from 'vue'
  4.     //引入需要使用的组件
  5. import {ElInput,ElForm,ElFormItem,ElButton,ElMessage } from 'element-plus'
  6. const form=reactive({
  7. name:'',
  8. password:''
  9. })
  10. function goNext(){
  11. console.log('123')
  12. ElMessage({
  13. type:"success",
  14. message: '登录成功',
  15. })
  16. router.push({
  17. path:"/home"
  18. })
  19. }
  20. </script>
  21. <template>
  22. <div class="login">
  23. <div class="login-box">
  24. <ElForm :model="form" label-width="80px">
  25. <el-form-item label="用户名">
  26. <el-input v-model="form.name"></el-input>
  27. </el-form-item>
  28. <el-form-item label="密码">
  29. <el-input v-model="form.password"></el-input>
  30. </el-form-item>
  31. </ElForm>
  32. <el-button class="loginbox-btn" size="large" @click="goNext">登录</el-button>
  33. </div>
  34. </div>
  35. </template>
  36. <style lang="scss">
  37. .login{
  38. width: 100vw;
  39. height: 100vh;
  40. background-image: url('../../assets/login_back.jpg');
  41. background-size: cover;
  42. overflow: hidden;
  43. border-image-repeat:no-repeat;
  44. display: flex;
  45. justify-content: center; //x轴居中
  46. align-items:center; //y轴居中
  47. &-box{
  48. width: 300px;
  49. height: 300px;
  50. padding: 60px;
  51. background-color: rgba($color: #fff, $alpha: 0.8);
  52. display: flex;
  53. justify-content: space-between;
  54. flex-wrap: wrap;
  55. .el-button{
  56. width: 280px;
  57. }
  58. }
  59. }
  60. </style>

到此完成!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/数据灵魂/article/detail/62776
推荐阅读
相关标签
  

闽ICP备14008679号