搜尋
首頁web前端Vue.jsvue3+vite2+ts4建置專案環境規範

這篇文章跟大家分享一套規格的vue3 vite2 ts4前端工程化專案環境,希望對大家有幫助!

vue3+vite2+ts4建置專案環境規範

一套規格的vue3 vite2 ts4前端工程化專案環境 https://webvueblog.github.io/vue3-vite2-ts4/

(學習影片分享:vuejs教學

Vue 3 Typescript Vite

vue3-vite2-ts4

npm init @vitejs/app
vue
vue-ts
npm install
npm run dev

vue3+vite2+ts4建置專案環境規範

目錄結構如下

├── public              静态资源
├── src
│   ├── assets           资源目录(图片、less、css等)
│   ├── components       项目组件
│   ├── App.vue          主应用
│   ├── env.d.ts         全局声明
│   └── main.ts          主入口
├── .gitignore           git忽略配置
├── index.html           模板文件
├── package.json        依赖包/运行脚本配置文件
├── README.md
├── tsconfig.json        ts配置文件
├── tsconfig.node.json   ts配置文件
└── vite.config.ts       vite配置

每個目錄的作用後文都會提及

├── src
│   ├── router           路由配置
│   ├── stores           状态管理
│   ├── typings          ts公共类型
│   ├── utils            工具类函数封装
│   └── views            页面视图

指定解析路徑使用的path module需要先安裝@type/node

npm install @types/node --save-dev

打包功能

build: {
      outDir: 'dist',   // 指定打包路径,默认为项目根目录下的 dist 目录
      terserOptions: {
          compress: {
              keep_infinity: true,  // 防止 Infinity 被压缩成 1/0,这可能会导致 Chrome 上的性能问题
              drop_console: true,   // 生产环境去除 console
              drop_debugger: true   // 生产环境去除 debugger
          },
      },
      chunkSizeWarningLimit: 1500   // chunk 大小警告的限制(以 kbs 为单位)
}

存取程式碼規格

ESlint 被稱為下一代的JS Linter工具,能夠將JS 程式碼解析成AST 抽象語法樹,然後偵測AST 是否符合既定的規則。

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

TypeScirpt 官方決定全面採用ESLint 作為程式碼檢查的工具,並創建了一個新專案typescript-eslint,提供了TypeScript 檔案的解析器@typescript-eslint/parser 和相關的設定選項@typescript- eslint/eslint-plugin 等

使用scss 來增強css 的語法能力

yarn add sass
yarn add stylelint
yarn add stylelint-scss

#存取naive ui函式庫

Naive UI 是特別推薦的vue3 UI 函式庫(https://www.naiveui.com/zh-CN/os-theme)

存取vue-router

npm install vue-router --save
import {
    createRouter, createWebHashHistory, RouteRecordRaw,
} from 'vue-router'

const routes: Array<RouteRecordRaw> = [
    { path: &#39;/&#39;, name: &#39;Home&#39;, component: () => import(&#39;views/home/index.vue&#39;)}
]

const router = createRouter({
    history: createWebHashHistory(),    // history 模式则使用 createWebHistory()
    routes,
})

export default router
import { createApp } from &#39;vue&#39;
import App from &#39;./App.vue&#39;
import router from &#39;./router/index&#39;

const app = createApp(App as any)
app.use(router)

#存取狀態管理工具pinia

##pinia 是輕量級的狀態管理庫

npm install pinia --save

引入

在main.ts中引入

import { createPinia } from &#39;pinia&#39;

app.use(createPinia())

在src/stores下新建一個counters.ts檔案

import { defineStore } from &#39;pinia&#39;

export const useCounterStore = defineStore(&#39;counter&#39;, {
    state: () => {
        return {
            count: 0
        }
    },
    getters: {
        count() {
            return this.count
        }
    },
    actions: {
        increment() {
            this.count++
        }
    }
})
import { defineStore } from &#39;pinia&#39;

export const useCounterStore = defineStore(&#39;counter&#39;, () => {
    const count = ref(0)
    function increment() {
      count.value++
    }

    return { count, increment }
})
<script setup>
    import { useCounterStore } from &#39;@/stores/counter&#39;

    const counter = useCounterStore()
</script>
<template>
    <div @click="counter.increment()">
        {{ counter.count }}
    </div>
</template>
const counter = useCounterStore()
const { count } = counter

<div @click="counter.increment()">{{ count }}</div>

pinia很貼心的提供了storeToRefs方法,讓我們可以享受解構的樂趣:

const { count } = storeToRefs(counter)

##接入圖表庫echarts5安裝&引入

npm install echarts --save

在src/utils/下新建echarts.ts用來引入我們需要使用的元件

import * as echarts from &#39;echarts/core&#39;
import {
    BarChart,
    // 系列类型的定义后缀都为 SeriesOption
    BarSeriesOption,
    // LineChart,
    LineSeriesOption
} from &#39;echarts/charts&#39;
import {
    TitleComponent,
    // 组件类型的定义后缀都为 ComponentOption
    TitleComponentOption,
    TooltipComponent,
    TooltipComponentOption,
    GridComponent,
    GridComponentOption,
    // 数据集组件
    DatasetComponent,
    DatasetComponentOption,
    // 内置数据转换器组件 (filter, sort)
    TransformComponent,
    LegendComponent
} from &#39;echarts/components&#39;
import { LabelLayout, UniversalTransition } from &#39;echarts/features&#39;
import { CanvasRenderer } from &#39;echarts/renderers&#39;

// 通过 ComposeOption 来组合出一个只有必须组件和图表的 Option 类型
export type ECOption = echarts.ComposeOption<
    | BarSeriesOption
    | LineSeriesOption
    | TitleComponentOption
    | TooltipComponentOption
    | GridComponentOption
    | DatasetComponentOption
>

// 注册必须的组件
echarts.use([
    TitleComponent,
    TooltipComponent,
    GridComponent,
    DatasetComponent,
    TransformComponent,
    BarChart,
    LabelLayout,
    UniversalTransition,
    CanvasRenderer,
    LegendComponent
])

// eslint-disable-next-line no-unused-vars
const option: ECOption = {
    // ...
}

export const $echarts = echarts

就可以在頁面中使用了:

<script setup>
    import { onMounted } from &#39;vue&#39;
    import { $echarts, ECOption } from &#39;@/utils/echarts&#39;

    onMounted(() => {
        // 测试echarts的引入
        const ele = document.getElementById(&#39;echarts&#39;) as HTMLCanvasElement
        const myChart = $echarts.init(ele)
        const option: ECOption = {
            title: {
                text: &#39;ECharts 入门示例&#39;
            },
            tooltip: {},
            legend: {
                data: [&#39;销量&#39;]
            },
            xAxis: {
                data: [&#39;衬衫&#39;, &#39;羊毛衫&#39;, &#39;雪纺衫&#39;, &#39;裤子&#39;, &#39;高跟鞋&#39;, &#39;袜子&#39;]
            },
            yAxis: {},
            series: [
                {
                    name: &#39;销量&#39;,
                    type: &#39;bar&#39;,
                    data: [5, 20, 36, 10, 10, 20]
                }
            ]
        }     
</script>

#配置統一axios 處理安裝&引入

npm install axios --save

截圖:

vue3+vite2+ts4建置專案環境規範(學習影片分享:

web前端開發

程式設計入門

以上是vue3+vite2+ts4建置專案環境規範的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:掘金社区。如有侵權,請聯絡admin@php.cn刪除
vue.js vs.後端框架:澄清區別vue.js vs.後端框架:澄清區別Apr 25, 2025 am 12:05 AM

Vue.js是前端框架,後端框架用於處理服務器端邏輯。 1)Vue.js專注於構建用戶界面,通過組件化和響應式數據綁定簡化開發。 2)後端框架如Express、Django處理HTTP請求、數據庫操作和業務邏輯,運行在服務器上。

vue.js和前端堆棧:了解連接vue.js和前端堆棧:了解連接Apr 24, 2025 am 12:19 AM

Vue.js與前端技術棧緊密集成,提升開發效率和用戶體驗。 1)構建工具:與Webpack、Rollup集成,實現模塊化開發。 2)狀態管理:與Vuex集成,管理複雜應用狀態。 3)路由:與VueRouter集成,實現單頁面應用路由。 4)CSS預處理器:支持Sass、Less,提升樣式開發效率。

Netflix:探索React(或其他框架)的使用Netflix:探索React(或其他框架)的使用Apr 23, 2025 am 12:02 AM

Netflix選擇React來構建其用戶界面,因為React的組件化設計和虛擬DOM機制能夠高效處理複雜界面和頻繁更新。 1)組件化設計讓Netflix將界面分解成可管理的小組件,提高了開發效率和代碼可維護性。 2)虛擬DOM機制通過最小化DOM操作,確保了Netflix用戶界面的流暢性和高性能。

vue.js和前端:深入研究框架vue.js和前端:深入研究框架Apr 22, 2025 am 12:04 AM

Vue.js被開發者喜愛因為它易於上手且功能強大。 1)其響應式數據綁定係統自動更新視圖。 2)組件系統提高了代碼的可重用性和可維護性。 3)計算屬性和偵聽器增強了代碼的可讀性和性能。 4)使用VueDevtools和檢查控制台錯誤是常見的調試技巧。 5)性能優化包括使用key屬性、計算屬性和keep-alive組件。 6)最佳實踐包括清晰的組件命名、使用單文件組件和合理使用生命週期鉤子。

vue.js在前端的力量:關鍵特徵和好處vue.js在前端的力量:關鍵特徵和好處Apr 21, 2025 am 12:07 AM

Vue.js是一個漸進式的JavaScript框架,適用於構建高效、可維護的前端應用。其關鍵特性包括:1.響應式數據綁定,2.組件化開發,3.虛擬DOM。通過這些特性,Vue.js簡化了開發過程,提高了應用性能和可維護性,使其在現代Web開發中備受歡迎。

vue.js比反應好嗎?vue.js比反應好嗎?Apr 20, 2025 am 12:05 AM

Vue.js和React各有優劣,選擇取決於項目需求和團隊情況。 1)Vue.js適合小型項目和初學者,因其簡潔和易上手;2)React適用於大型項目和復雜UI,因其豐富的生態系統和組件化設計。

vue.js的功能:增強前端的用戶體驗vue.js的功能:增強前端的用戶體驗Apr 19, 2025 am 12:13 AM

Vue.js通過多種功能提升用戶體驗:1.響應式系統實現數據即時反饋;2.組件化開發提高代碼復用性;3.VueRouter提供平滑導航;4.動態數據綁定和過渡動畫增強交互效果;5.錯誤處理機制確保用戶反饋;6.性能優化和最佳實踐提升應用性能。

vue.js:定義其在網絡開發中的作用vue.js:定義其在網絡開發中的作用Apr 18, 2025 am 12:07 AM

Vue.js在Web開發中的角色是作為一個漸進式JavaScript框架,簡化開發過程並提高效率。 1)它通過響應式數據綁定和組件化開發,使開發者能專注於業務邏輯。 2)Vue.js的工作原理依賴於響應式系統和虛擬DOM,優化性能。 3)實際項目中,使用Vuex管理全局狀態和優化數據響應性是常見實踐。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具