国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

從0開始搭建一個vue3+vite+ts+pinia+element-plus的項目

這篇具有很好參考價值的文章主要介紹了從0開始搭建一個vue3+vite+ts+pinia+element-plus的項目。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言:vue3+ts+vite大家已經(jīng)都開始用了,最近也在學(xué)習(xí),基本上是零基礎(chǔ)開始ts的學(xué)習(xí),很多語法知識是邊寫邊查,沒有系統(tǒng)的學(xué)習(xí)ts。此處展示從零開始,搭建的一個框架,方便拿來即用!

1. 初始化一個vite項目

npm create vite@latest

其中框架選擇vue,語言選擇typeScript

2. 啟動項目

npm install
npm run dev

項目啟動成功以后如下所示:
從0開始搭建一個vue3+vite+ts+pinia+element-plus的項目

3. 修改目錄

為了方便日常工作中的框架使用,在此處對剛初始化好的框架進(jìn)行改造,在原有框架的基礎(chǔ)上,添加store,router,layout,utils,views等文件夾,其中的作用將在后面進(jìn)行說明。如圖所示:
從0開始搭建一個vue3+vite+ts+pinia+element-plus的項目

4. 配置router

首先安裝vue-router的依賴

npm install vue-router

在router文件夾下創(chuàng)建index.ts文件,配置如下:

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
const routes: Array<RouteRecordRaw> = [
    // 項目打開后進(jìn)入的默認(rèn)地址
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: () => import('../views/login/login.vue')
    },
    {
        path: '/home',
        component: () => import('../views/home/home.vue')
    }
]
const router = createRouter({
    history: createWebHashHistory(), // 本項目采用了哈希模式
    routes
})
export default router

路由文件配置以后,需要在main.ts文件中進(jìn)行引用,如下所示:

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
const app = createApp(App)
// 路由設(shè)置
import router from './router/index'
app.use(router)

app.mount('#app')

修改app.vue文件,根據(jù)實際情況進(jìn)行修改,本項目中不需要初始化的helloWord組件,所以修改如下所示:

<script setup lang="ts">
</script>
<template>
  <div>
    <router-view></router-view>
  </div>
</template>
<style scoped>
</style>

在views文件夾中創(chuàng)建login.vue文件,并重新啟動項目,如圖所示,跳轉(zhuǎn)到登陸頁面后表示路由配置成功:
從0開始搭建一個vue3+vite+ts+pinia+element-plus的項目
vue文件中使用路由的方法如下:

import { useRouter, useRoute} from 'vue-router'
const router = useRouter()
const route = useRoute()
// 路由跳轉(zhuǎn)
router.push('/xxx')

**補充:**路由hash模式與history模式的區(qū)別

  1. hash模式
    示例:http://localhost:5173/#/login
    原理:利用window監(jiān)聽的onhashchange事件,不會包含在http請求中,對后端完全沒有影響。
  2. history模式
    示例:http://localhost:5173/login
    原理:地址發(fā)生改變后,會按照改變后的地址向服務(wù)端發(fā)送請求,需要后端配合處理,做地址映射。

5.配置vuex(pinia)

pinia 是一個擁有組合式API的狀態(tài)管理庫。
pinia官網(wǎng): https://pinia.vuejs.org/zh/introduction.html
首先安裝pinia的依賴

npm install pinia

在store文件夾中創(chuàng)建index.ts,具體內(nèi)容如下:

import type {App} from 'vue'
import { createPinia } from "pinia";
const store = createPinia()
export function setupStore(app:App<Element>) {
    app.use(store)
}
export {store}

然后在store文件夾中創(chuàng)建modules文件夾,并創(chuàng)建user.ts 文件,用來保存用戶的基本信息。

import { defineStore } from 'pinia'
import { store } from '../index'
interface userInfoType {
    username: string,
    age: number,
    gender: string
}
export const useUserStore = defineStore('user', () => {
// 此處使用的是組合式API的方式,更多情況可查看官網(wǎng)
    let userInfo: userInfoType = {
        username: '小明',
        age: 18,
        gender: '男'
    }
    return {
        userInfo
    }
},
)
// 最后到處定義的useUserStore
export function useUserStoreHook() {
    return useUserStore(store)
}

store的基本信息配置好以后,需要在main.ts問價中掛載到app組件上,如下:

import { setupStore } from './store'
// 掛載store
setupStore(app);

存儲在vuex中的數(shù)據(jù)使用方式如下:

// login.vue 文件
<script setup lang="ts">
import { useUserStore } from '../../store/modules/user'
const userStore = useUserStore()
let userInfo = userStore.userInfo
</script>
<template>
  <div class="login-wrap">
    <p>登錄頁面</p>
    {{ userInfo.username }}的年齡是{{ userInfo.age }}
  </div>
</template>

<style scoped>
.login-wrap {
  width: 100%;
  height: 100vh;
  background-size: 100% 100%;
  position: relative;
  box-sizing: border-box;

}
</style>

效果如下:
從0開始搭建一個vue3+vite+ts+pinia+element-plus的項目

6. element-plus 的基本使用

首先安裝依賴

npm install element-plus

官網(wǎng): https://element-plus.org/zh-CN/guide/design.html
本項目中使用的是方法是在main.ts文件中完整導(dǎo)入!main.ts文件如下:

// element-plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// 導(dǎo)入所有的icon圖標(biāo)
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
app.use(ElementPlus)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

在vue文件中基本使用方法:

import { ElMessage } from 'element-plus'
ElMessage('你好')

7. axios封裝

對于前后分離的項目來說,綁定后端提供的接口進(jìn)行數(shù)據(jù)增刪改查是業(yè)務(wù)中較為重要的部分,此處對axios的使用進(jìn)行封裝,方便向服務(wù)斷發(fā)送請求。
依舊是安裝axios的依賴:

npm install axios

axios的接口封裝文件如下:

import axios from 'axios';
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
import {getCookie,setCookie} from '../../utils/cookie'
const service: AxiosInstance = axios.create({
    baseURL: import.meta.env.VITE_APP_API_BASEURL,
    timeout: 15000,
    headers: {
        'Content-Type': 'application/json'
    },
});

interface requestConfig extends AxiosRequestConfig {
    headers?: any
}
// axios實例攔截請求
service.interceptors.request.use(
    (config: requestConfig) => {
        const token = getCookie('token');
        if (token) {
            config.headers.token = token
        }
        return config;
    },
    (error: AxiosError) => {
        return Promise.reject(error);
    },
);
// axios實例攔截響應(yīng)
service.interceptors.response.use(
    (response: AxiosResponse) => {
        if (response.status === 200) {
            return response;
        }
        return response;
    },
    // 請求失敗
    (error: any) => {
        const { response } = error;
        if (response) {
            // 請求已發(fā)出,但是不在2xx的范圍
            return Promise.reject(response.data);
        }
    },
);

/***
 * post 方法封裝
 */
export function postMethod<T>(url:string,params?:any):Promise<T> {
    return new Promise((resolve, reject) => {
        service.post(url,params).then((res:any) => {
            if(res.data.code === 200  && res.headers['token'])  {
                setCookie('token',res.headers['token'],21600)
            }
            resolve(res.data)
        }).catch((err) => {
            reject(err)
        })
    })
}
/***
 * get 方法封裝
 */
export function getMethod<T>(url:string,params?:any):Promise<T> {
    return new Promise((resolve, reject) => {
        service.get(url,params).then((res:any) => {
            if(res.data.code === 200  && res.headers['token'])  {
                setCookie('token',res.headers['token'],21600)
            }
            resolve(res.data)
        }).catch((err) => {
            reject(err)
        })
    })
}
/**
 * 文件上傳 方法封裝
 * 上傳文件傳參方式注意為form-data格式
 */
export function uploadFile<T>(url:string,uploadForm:any):Promise<T>{
    const fd = new FormData()
    fd.append('fileName', uploadForm.projectId)
    fd.append('file', uploadForm.file)
    return new Promise((resolve, reject) => {
        service.post(url,fd,{
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }).then((res:any) => {
            if(res.data.code === 200  && res.headers['token'])  {
                setCookie('token',res.headers['token'],21600)
            }
            resolve(res.data)
        }).catch((err) => {
            reject(err)
        })
    })
}

8. cookie封裝

用戶登錄的時候,提交表單信息,需要對接口返回的token進(jìn)行存儲,此處使用了cookie進(jìn)行存儲,可以利用js-cookie庫進(jìn)行cookie存儲,也可自己封裝方法,此處是封裝cookie的方法進(jìn)行cookie的存儲,獲取,以及清除,如下所示:

// cookie.ts
/**
 * cookie 瀏覽器緩存
 */
const domain = ''
//  時間單位為秒
export function setCookie(c_name: string, value: any, expire?: any) {
  var date: any = new Date()
  date.setSeconds(date.getSeconds() + expire)
  document.cookie = c_name + '=' + escape(value) + '; expires=' + date.toGMTString() + '; domain=' + domain + ';path=/'
}

export function getCookie(c_name: string) {
  let c_start: number
  let c_end: number
  if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf(c_name + '=')
    if (c_start != -1) {
      c_start = c_start + c_name.length + 1
      c_end = document.cookie.indexOf(';', c_start)
      if (c_end == -1) c_end = document.cookie.length
      return unescape(document.cookie.substring(c_start, c_end))
    }
  }
  return ''
}
export function delCookie(c_name:string) {
  setCookie(c_name, '', -1)
}

9. localStorage 使用

在獲取localStorage存儲信息時,發(fā)現(xiàn)存在一個問題,在vue文件中的setup的語法糖中,直接使用JSON的序列化方法會標(biāo)紅,可以正常使用,但是存在類型問題。將其進(jìn)行封裝后,發(fā)現(xiàn)這個問題不在出現(xiàn),在vue文件中使用時,直接引入即可。如下所示:

// localStorage.ts
/**
 * window.localStorage 瀏覽器永久緩存
 */
export const localStorage = {
  // 設(shè)置永久緩存
  set(key: string, val: any) {
    window.localStorage.setItem(key, JSON.stringify(val));
  },
  // 獲取永久緩存
  get(key: string) {
    const json: any = window.localStorage.getItem(key);
    return JSON.parse(json);
  },
  // 移除永久緩存
  remove(key: string) {
    window.localStorage.removeItem(key);
  },
  // 移除全部永久緩存
  clear() {
    window.localStorage.clear();
  }
};

10. 框架地址

gitee地址: https://gitee.com/free_project/vue3_ts_frame.git
目前只有一個空架子,針對表單的使用等還未進(jìn)行展示;因為對ts不熟,好多類型都設(shè)置了any。若有問題還請指出!文章來源地址http://www.zghlxwxcb.cn/news/detail-464394.html

到了這里,關(guān)于從0開始搭建一個vue3+vite+ts+pinia+element-plus的項目的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 從零開始創(chuàng)建一個vue3+vite項目并集成element-plus、eslint以及prettier

    從零開始創(chuàng)建一個vue3+vite項目并集成element-plus、eslint以及prettier

    項目git地址, 歡迎修改提交,不足地方還請補充批評指正! 項目git地址 ESLint 是一個用于識別和報告在 ECMAScript/JavaScript 代碼中發(fā)現(xiàn)的模式的工具,其目標(biāo)是使代碼更加一致并避免錯誤。 ESLint 是完全插件化的。每條規(guī)則都是一個插件,你可以在運行時添加更多。你還可以添

    2024年04月09日
    瀏覽(27)
  • 【Vue H5項目實戰(zhàn)】從0到1的自助點餐系統(tǒng)—— 搭建腳手架(Vue3.2 + Vite + TS + Vant + Pinia + Node.js)

    【Vue H5項目實戰(zhàn)】從0到1的自助點餐系統(tǒng)—— 搭建腳手架(Vue3.2 + Vite + TS + Vant + Pinia + Node.js)

    H5 項目基于 Web 技術(shù),可以在智能手機(jī)、平板電腦等移動設(shè)備上的瀏覽器中運行,無需下載和安裝任何應(yīng)用程序,且H5 項目的代碼和資源可以集中在服務(wù)器端進(jìn)行管理,只需更新服務(wù)器上的代碼,即可讓所有顧客訪問到最新的系統(tǒng)版本。 本系列將以肯德基自助點餐頁面為模板

    2024年02月13日
    瀏覽(130)
  • vue3+ts+pinia+vite一次性全搞懂

    前提是所處vue環(huán)境為vue3,如果失敗就查看一下環(huán)境是否為vue2,然后刪除vue2,安裝vue3 這是我報過的錯

    2024年02月01日
    瀏覽(23)
  • 詳解Vite創(chuàng)建Vue3項目+vue-router+ts+vite+element-plus

    詳解Vite創(chuàng)建Vue3項目+vue-router+ts+vite+element-plus

    前言 在之前的文章中寫過“Vue3+TS+ElementPlus的安裝和使用教程【詳細(xì)講解】”,但那篇文章寫的是創(chuàng)建vue3的項目沒有使用到Vite構(gòu)建工具進(jìn)行創(chuàng)建還是使用的常規(guī)webpacket構(gòu)建工具進(jìn)行創(chuàng)建的。提到Vite和webpacket的時候我們可以簡單說一下。 Vite 和 Webpack 都是現(xiàn)代化的前端構(gòu)建工

    2024年01月18日
    瀏覽(21)
  • Vue3+TS+Vite創(chuàng)建項目,并導(dǎo)入Element-plus和Sass

    Vue3+TS+Vite創(chuàng)建項目,并導(dǎo)入Element-plus和Sass

    1.桌面新建一個文件夾Vue3-app 打開編輯器導(dǎo)入文件夾,編輯器打開終端輸入或者命令行工具cd到項目目錄下輸入 npm init vue@latest 回車運行 這里我選擇了TS+Vite來開發(fā),并選擇安裝路由 2.cd到 vue-project目錄下 輸入 npm install 回車運行 3.安裝完成后 輸入 npm run dev 回車運行 瀏覽器打開

    2024年02月16日
    瀏覽(23)
  • vite + vue3 + vue-router4 + ts + element plus + pinia + axios構(gòu)建項目

    最后是完整的vite.config.ts、main.ts配置 1、先用vite創(chuàng)建一個項目 2、安裝element plus vite.config.ts配置組件按需導(dǎo)入,圖標(biāo)自動導(dǎo)入 ? main.ts配置 頁面文件使用 3、安裝axios main.ts 4、安裝pinia /stores/index.ts /stores/counter.ts main.ts 頁面使用 5、安裝router4 /router/index main.ts ? 6、vite.config.ts常

    2023年04月25日
    瀏覽(27)
  • Vue3 Vite4 ElementPlus TS模板(含Vue-Router4+Pinia4)

    Vue3 Vite4 ElementPlus TS模板(含Vue-Router4+Pinia4)

    手動安裝配置Vue3 ElementPlus模板比較繁瑣,網(wǎng)上尋找一些模板不太符合自己預(yù)期,因此花點精力搭建一個符合自己需求的架子 采用最新的組件,版本如下: vite 4.3.9 vite-plugin-mock 2.9.8 vue 3.3.4 pinia 2.1.3 vue-router 4.2.2 element-plus 2.3.6 滿足自己以下功能: Vite工具熱啟動速度快,修改后

    2024年02月08日
    瀏覽(24)
  • 前端新手Vue3+Vite+Ts+Pinia+Sass項目指北系列文章 —— 第一章 技術(shù)棧簡介 (開篇)

    前端新手Vue3+Vite+Ts+Pinia+Sass項目指北系列文章 —— 第一章 技術(shù)棧簡介 (開篇)

    旨在幫助初學(xué)者掌握使用現(xiàn)代前端技術(shù)棧構(gòu)建應(yīng)用的基礎(chǔ)知識和技能。在這個系列中,我們將深入探討如何結(jié)合Vue.js、Vite、TypeScript、Pinia和Sass這些強大的工具和框架來開發(fā)現(xiàn)代化的前端應(yīng)用。 通過這個系列,我們將從零開始構(gòu)建一個完整的前端項目,覆蓋項目初始化、組件

    2024年02月05日
    瀏覽(30)
  • Vue3.2 + TypeScript + Pinia + Vite4 + Element-Plus + 微前端(qiankun) 后臺管理系統(tǒng)模板(已開源---顯示項目頁面截圖)

    Vue3.2 + TypeScript + Pinia + Vite4 + Element-Plus + 微前端(qiankun) 后臺管理系統(tǒng)模板(已開源---顯示項目頁面截圖)

    Wocwin-Admin,是基于 Vue3.2、TypeScript、Vite、Pinia、Element-Plus、Qiankun(微前端) 開源的一套后臺管理模板;同時集成了微前端 qiankun也可以當(dāng)做一個子應(yīng)用。項目中組件頁面使用了Element-plus 二次封裝 t-ui-plus 組件,目前已新增fastmock接口。 Link:https://wocwin.github.io/wocwin-admin/ 賬號:

    2024年02月08日
    瀏覽(39)
  • Vite4+Typescript+Vue3+Pinia 從零搭建(3) - vite配置

    Vite4+Typescript+Vue3+Pinia 從零搭建(3) - vite配置

    項目代碼同步至碼云 weiz-vue3-template 關(guān)于vite的詳細(xì)配置可查看 vite官方文檔,本文簡單介紹vite的常用配置。 項目初建后, vite.config.ts 的默認(rèn)內(nèi)容如下: 比如,修改 App.vue : 根目錄下新建 .env 、 .env.development 、 .env.production 三個文件。 .env 文件內(nèi)新增內(nèi)容: .env.development 文件內(nèi)

    2024年02月05日
    瀏覽(81)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包