Vue3 + Vite + Typescript + Element Plus创建项目

31 篇文章 2 订阅
16 篇文章 1 订阅

1、体验对比

1、启动

  • vue-cli:
    在这里插入图片描述

  • vite:
    在这里插入图片描述

2、打包

  • vue-cli:
    在这里插入图片描述

  • vite:在这里插入图片描述

2、vue-cli

vue create vue3-cli

  • Default Vue 3
  • or
  • Manually select features
    在这里插入图片描述

3、vite

npm i -g create-vite-app
yarn create @vitejs/app

在这里插入图片描述

yarn create vite hello-vue3 --template vue

  • 项目结构
vue3-admin
├─.eslintignore
├─.eslintrc.js
├─.gitignore
├─.prettierrc.js
├─README.md
├─auto-imports.d.ts
├─components.d.ts
├─index.html
├─package.json
├─tsconfig.json
├─vite.config.ts
├─src
|  ├─App.vue
|  ├─env.d.ts
|  ├─main.ts
|  ├─views
|  |   ├─login
|  |   |   └index.vue
|  ├─styles
|  |   ├─main.css
|  |   └reset.css
|  ├─store
|  |   └index.ts
|  ├─router
|  |   └index.ts
|  ├─components
|  |     └HelloWorld.vue
|  ├─assets
|  |   └logo.png
├─public
|   ├─favicon.ico
1、Eslint & Prettier

yarn add eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin -D

module.exports = {
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser', 
    ecmaVersion: 2020, 
    sourceType: 'module', 
    ecmaFeatures: {
      jsx: true
    }
  },
  extends: [
    'plugin:vue/vue3-recommended',
    'plugin:@typescript-eslint/recommended', 
    'plugin:prettier/recommended'
  ],
  rules: {}
}

yarn add prettier eslint-config-prettier eslint-plugin-prettier -D

// 具体配置可以参考 https://prettier.io/docs/en/options.html
module.exports = {
  printWidth: 100,
  tabWidth: 4,
  semi: false, // 未尾逗号
  singleQuote: true, // 单引号
  bracketSpacing: true,
  trailingComma: 'none', // 未尾分号
  proseWrap: 'never',
  htmlWhitespaceSensitivity: 'strict'
};
2、添加eslint相关命令

skipLibCheck 增加skipLibCheck可以跳过引入库的ts检查

{
  ...
  "scripts": {
    "build": "vue-tsc --noEmit --skipLibCheck && vite build",
    "lint": "eslint src",
    "lint:fix": "eslint src --fix --ext .ts,.tsx"
  },
}
3、配置路径别名

vite.config.ts文件

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path')

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [vue()],
    resolve: {
        alias: {
            "@": path.resolve(__dirname, "/src"),
        }
    }
})

tsconfig.json文件

{
  "compilerOptions": {
    // ...
    "baseUrl": "src",
    "paths": {
      "@/*": ["*"],
    },
  },
  // ...
}
4、less / scss 配置

yarn add less less-loader -D
yarn add sass sass-loader node-sass -D

5、router配置

安装最新的版本

yarn add vue-router@4.0.12

  • 创建router/index.ts
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/home',
        name: 'home',
        meta: {
            type: 'home'
        },
        component: () => import('@/views/home.vue')
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            type: 'login'
        },
        component: () => import('@/views/login/index.vue')
    },
    {
        path: '/:pathMatch(.*)*',
        name: '404',
        component: () => import('@/views/404.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(), 
    routes
})

export default router
6、element-plus 组件库

yarn add element-plus

按需导入组件库样式插件

yarn add unplugin-vue-components unplugin-auto-import -D

  • vite.config.ts文件
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default {
    plugins: [
        // ...
        AutoImport({
            resolvers: [ElementPlusResolver()],
        }),
        Components({
            resolvers: [ElementPlusResolver()],
        }),
    ],
}
7、写法示例

login.vue

<template>
    // ...
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { ElForm } from 'element-plus'

const ruleFormRef = ref<InstanceType<typeof ElForm>>()
interface FormData {
    account: string
    password: string
}
const ruleForm = reactive<FormData>({
    account: '',
    password: ''
})

const loginRules = reactive({
    account: [
        {
            required: true,
            message: '企业账号不能为空',
            trigger: 'blur'
        }
    ],
    password: [
        {
            required: true,
            message: '密码不能为空',
            trigger: 'blur'
        }
    ]
})

interface StatsBase {
    loading: boolean
    isSecure: boolean
}
const state = reactive<StatsBase>({
    loading: false,
    isSecure: true
})

const setSecure = () => {
    state.isSecure = !state.isSecure
    console.log(state.isSecure)
}

const submitForm = async (ruleFormRef: any) => {
    ruleFormRef.value.validate((valid: boolean) => {
        if (valid) {
        }
    })
}
</script>

<style lang="scss" scoped>

</style>
8、更多
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue3 是一种用于构建用户界面的开源JavaScript 框架。它提供了许多功能和工具,使用户能够更轻松地创建可交互、响应式的应用程序。Vue3 采用了一些新的语法和技术,使其在性能和开发效率方面有了明显的改进。 Vite4 是一个基于原生ES模块解析的构建工具。它具有快速冷启动和快速热更新的能力,使开发人员能够更快速地进行开发和调试。与传统的打包工具不同,Vite4 通过在浏览器运行时进行模块解析,避免了不必要的打包和编译步骤,提供了更加流畅的开发体验。 Element Plus 是一个基于Vue3 的组件库,提供了丰富的UI组件和交互式工具,可以帮助开发人员快速构建出现代化的Web界面。Element Plus 是Element UI 的升级版,针对Vue3 进行了优化和改进,提供了更好的性能和更丰富的功能。 TypeScript 是一种强类型的JavaScript 超集,允许开发人员在JavaScript 代码中添加静态类型注解。这样可以在开发过程中提早发现错误并提供更好的代码提示。Vue3 通过与TypeScript 的集成,使开发人员能够使用类、接口、泛型等高级特性来构建更健壮的应用程序。 综上所述,Vue3、Vite4、Element Plus 和 TypeScript 的结合,可以提供一个高效、灵活和强大的开发环境。开发人员可以利用Vite4 的快速构建能力和热更新功能,结合Element Plus 提供的丰富组件,通过TypeScript 的类型检查和提示,更加轻松地构建出现代化的Vue 应用程序。 ### 回答2: Vue 3 是一款非常流行的 JavaScript 框架,用于构建现代化的用户界面。它的核心思想是组件化开发,通过将界面拆分成独立的组件,使开发者能够更好地管理和重用代码。 Vite 4 是 Vue 3 的一个新型构建工具,用于快速构建现代化的前端应用。相比传统的打包工具,Vite 4 支持开箱即用的单文件组件,可以进行更快的热重载和快速启动,从而提高开发效率。 Element Plus 是一套基于 Vue 3 的UI组件库,它提供了大量的高质量的组件,可以用于构建漂亮、响应式的用户界面。Element Plus 的组件易于使用,灵活且高度可定制,适合开发各种类型的应用。它还提供了强大的工具和样式库,使开发者能够更轻松地定制和管理应用的外观和风格。 TypeScript一个强类型的 JavaScript 超集,它为开发者提供了更强大的类型检查和代码提示功能。与 JavaScript 不同,TypeScript 在编码过程中能够捕获一些常见的错误,帮助开发者提高代码的质量和可维护性。TypeScript 还支持最新的 JavaScript 特性,并且可以与 Vue 3、Vite 4 和 Element Plus 等工具和库完美集成。 综合上述所述,结合 Vue 3、Vite 4、Element Plus 和 TypeScript 可以实现一种高效、可维护和易于定制的开发方式。开发者可以使用 Vite 4 快速创建 Vue 3 项目,同时借助 Element Plus 的丰富组件库来构建出漂亮的用户界面。而 TypeScript 则提供了更强大的类型检查和代码提示,避免一些潜在的bug,提高开发效率。通过组合使用这些工具和库,可以实现更快速、更可靠的前端开发体验。 ### 回答3: Vue3是一种流行的JavaScript框架,它具有响应式和组件化的特性,使开发者可以轻松构建现代化的Web应用程序。Vite4是Vue3的一个新的构建工具,旨在提供更快的启动和热重载,以及更好的开发体验。 Element Plus是一个基于Vue3的UI库,它提供了一套美观、易用和高性能的组件,用于构建优雅的用户界面。它使用了TypeScript来提供类型检查和智能提示,这使得在使用Element Plus时能更加轻松地进行开发。 TypeScript是一种由微软开发的编程语言,它是JavaScript的超集,添加了静态类型支持。它允许开发者在编代码时进行类型检查,并提供了更好的代码补全和智能感知功能。在Vue3和Element Plus使用TypeScript可以提高代码的可读性和可维护性,并减少错误和调试时间。 综上所述,Vue3、Vite4、Element Plus和TypeScript是一组强大的工具和库,可以帮助开发者更快速、高效地构建现代化的Web应用程序。它们的结合提供了优秀的开发体验,并能够轻松满足各种UI需求。无论是构建大型企业应用还是小型个人项目,它们都是很好的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值