欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基礎(chǔ)的前端vite項(xiàng)目創(chuàng)建過程詳解

 更新時(shí)間:2024年11月19日 11:18:34   作者:黃彥祺  
這篇文章主要介紹了如何使用Vite創(chuàng)建一個(gè)前端項(xiàng)目,并配置了Vue?Router、Vuex、Element?Plus、Axios和Element?Plus圖標(biāo)等第三方依賴,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

1. 準(zhǔn)備環(huán)境

確保你的計(jì)算機(jī)上已安裝Node.js和npm(或yarn,如果你更偏好使用yarn)。你可以通過運(yùn)行node -vnpm -v(或yarn -v)來檢查它們是否已安裝以及安裝的版本。

2. 安裝Vite

在命令行(終端)中,使用npm(或yarn)全局安裝Vite。雖然對(duì)于單個(gè)項(xiàng)目來說,全局安裝不是必需的,但這樣做可以確保你可以在任何地方使用Vite命令。

3.創(chuàng)建一個(gè)vite前端項(xiàng)目

yarn create vite 項(xiàng)目名 --template vue

4. 進(jìn)入到創(chuàng)建的項(xiàng)目路徑

cd  項(xiàng)目名

 yarn //安裝依賴

5.安裝配置項(xiàng)目所需的第三方依賴

第三方依賴vue-router,vuex ,element-plus, axios ,qs ,element-plus-icon是vite基礎(chǔ)項(xiàng)目的必須依賴,其他依賴可根據(jù)自己實(shí)際需求來安裝。

5.1.配置路由

5.1.1.安裝路由:

yarn  add   vue-router

5.1.2 .vue-router的配置 

在src創(chuàng)建router目錄, 在router目錄創(chuàng)建index.js,將以下基本內(nèi)容復(fù)制粘貼。

import { createRouter, createWebHistory} from 'vue-router'?
?
const routes = [
];
?
const router = createRouter({
        routes,  //路由規(guī)則
        history:  createWebHistory(),
        linkActiveClass:'active'
    });
?
//全局前置路由守衛(wèi)?
?
export default router;

在main.js文件中配置router

import router from './router'
app.use(router)

5.2.配置vuex (全局的狀態(tài)管理)

5.2.1.安裝vuex

yarn add vuex

5.2.2.vuex的配置

在src目錄下創(chuàng)建store目錄, 在store目錄創(chuàng)建一個(gè)index.js

//1.導(dǎo)入createStore函數(shù)
import {createStore} from 'vuex'?
?
//2.創(chuàng)建vuex的核心對(duì)象
//定義一個(gè)狀態(tài)
const  state={
}
//state的計(jì)算屬性
const getters={
?
}
?
//修改狀態(tài)的  同步的
const  mutations ={
?
}
?
//actions  操作  定義事件,讓組件觸發(fā)事件
const actions = {
   
       
}
?
const plugins =[]
?
//3. 調(diào)用createStore創(chuàng)建store對(duì)象
const store = createStore({
    state,                 
    mutations,
    actions,
    getters,
    plugins,
});
?
//4.暴露store
export default store;

在main.js配置store

import store from './store'
app.use(store)

5.3.配置element-plus

5.3.1 .安裝element-plus

yarn add  element-plus

5.3.2.在main.js配置

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
app.use(ElementPlus)

5.4.配置element-plus圖標(biāo)

5.4.1.安裝element-plus圖標(biāo)

yarn add @element-plus/icons-vue

5.4.2. 在main.js配置

import * as ElementPlusIconsVue from '@element-plus/icons-vue'
?
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

5.5.配置axios

5.5.1 安裝axios和qs

yarn add  axios
yarn add   qs

5.5.2.axios的配置

在src目錄創(chuàng)建一個(gè)http目錄, 創(chuàng)建兩個(gè)文件

1.axios實(shí)例配置文件: config.js,將以下基本配置復(fù)制粘貼

//axios的配置文件
export default {
    method: 'get',
    // 基礎(chǔ)url前綴
    baseUrl: 'http://localhost:8080',//根據(jù)項(xiàng)目進(jìn)行修改
    // 請(qǐng)求頭信息
    headers: {
      //默認(rèn)的請(qǐng)求context-type: application/json
      'Content-Type': 'application/json;charset=UTF-8'
    },
    // 參數(shù)
    data: {},
    // 設(shè)置超時(shí)時(shí)間
    timeout: 10000,
    // 攜帶憑證  是否攜帶cookie
    withCredentials: true,
    // 返回?cái)?shù)據(jù)類型
    responseType: 'json'
  }

2.創(chuàng)建一個(gè)request.js 封裝axios 工具庫

import { ElLoading,ElMessage } from 'element-plus'
import axios from 'axios'
import qs from 'qs'  //把json進(jìn)行序列化成key/value
import config from './config'
import  $router from '../router'
?
const instance = axios.create({
    baseURL: config.baseUrl,
    headers: config.headers,
    timeout: config.timeout,
    withCredentials: config.withCredentials
  })
// request 攔截器
instance.interceptors.request.use(
    config => {
      let token = sessionStorage.getItem("token");
      // 帶上token
      if (token) {
        config.headers.token = token
      }
      return config
    });
?
const request = async function (loadtip, query) {
    let loading
    if (loadtip) {
        loading = ElLoading.service({
            lock: true,
            text: '正在加載...',
            background: 'rgba(0, 0, 0, 0.7)',
        })
    }
    const res = await instance.request(query)
    if (loadtip) {
        loading.close()
    }
    if (res.data.meta.status === 401) {
        //ElMessage.error();
        $router.push({ path: '/login' })
        return Promise.reject(res.data) //reject()  catch捕獲
    } else if (res.data.meta.status === 500) {
        return Promise.reject(res.data)
    } else if (res.data.meta.status === 501) {
        return Promise.reject(res.data)
    } else if (res.data.meta.status === 502) {
        $router.push({ path: '/login' })
        return Promise.reject(res.data)
    } else {
        return Promise.resolve(res.data)  // then()
    }
        /*
        .catch(e => {
            if (loadtip) {
                loading.close()
            }
            return Promise.reject(e.msg)
        })
        */
}
const get = function (url, params) {
    const query = {
        url: url,
        method: 'get',
        withCredentials: true,
        timeout: 30000,
        params: params,  //params: queryString
        headers: { 'request-ajax': true }
    }
    return request(false, query)
}
const post = function (url, params) {
    const query = {
        url: url,
        method: 'post',
        withCredentials: true,
        timeout: 30000,
        data: params,  //請(qǐng)求體
        headers: { 'Content-Type': 'application/json', 'request-ajax': true }
    }
    return request(false, query)
}
const postWithLoadTip = function (url, params) {
    const query = {
        url: url,
        method: 'post',
        withCredentials: true,
        timeout: 30000,
        data: params,
        headers: { 'Content-Type': 'application/json', 'request-ajax': true }
    }
    return request(true, query)
}
const postWithOutLoadTip = function (url, params) {
    const query = {
        url: url,
        method: 'post',
        withCredentials: true,
        timeout: 30000,
        data: params,
        headers: { 'Content-Type': 'application/json', 'request-ajax': true }
    }
    return request(false, query)
}
const postWithUrlEncoded = function (url, params) {
    const query = {
        url: url,
        method: 'post',
        withCredentials: true,
        timeout: 30000,
        data: qs.stringify(params), //params:json  qs.stringify(json) --> 轉(zhuǎn)換key/value
        headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'request-ajax': true }
    }
    return request(false, query)
}
?
const del = function (url, params) {
    const query = {
        url: url,
        method: 'DELETE',
        withCredentials: true,
        timeout: 30000,
        data: params,
        headers: { 'Content-Type': 'application/json', 'request-ajax': true }
    }
    return request(true, query)
}
const put = function (url, params) {
    const query = {
        url: url,
        method: 'PUT',
        withCredentials: true,
        timeout: 30000,
        data: params,
        headers: { 'Content-Type': 'application/json', 'request-ajax': true }
    }
    return request(true, query)
}?
?
const form = function (url, params) {
    const query = {
        url: url,
        method: 'post',
        withCredentials: true,
        timeout: 30000,
        data: params,
        headers: { 'Content-Type': 'multipart/form-data', 'request-ajax': true }
    }
    return request(false, query)
}?
?
export default {
    post,
    postWithLoadTip,
    postWithOutLoadTip,
    postWithUrlEncoded,
    get,
    form,
    del,
    put
}

3.在main.js配置

import $http from './http/request.js'
app.config.globalProperties.$http =  $http

總結(jié) 

到此這篇關(guān)于前端vite項(xiàng)目創(chuàng)建的文章就介紹到這了,更多相關(guān)前端vite項(xiàng)目創(chuàng)建內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解vue大文件視頻切片上傳的處理方法

    詳解vue大文件視頻切片上傳的處理方法

    前端上傳大文件、視頻的時(shí)候會(huì)出現(xiàn)超時(shí)、過大、很慢等情況,為了解決這一問題,跟后端配合做了一個(gè)切片的功能,接下來就詳細(xì)的給大家介紹一下vue大文件視頻切片上傳的處理方法,需要的朋友可以參考下
    2023-08-08
  • 詳解Vue中是如何實(shí)現(xiàn)cache緩存的

    詳解Vue中是如何實(shí)現(xiàn)cache緩存的

    這篇文章分享一個(gè)比較有意思的東西,那就是Vue中如何實(shí)現(xiàn)cache緩存的,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-07-07
  • uni-app中vue3表單校驗(yàn)失敗的問題及解決方法

    uni-app中vue3表單校驗(yàn)失敗的問題及解決方法

    最近遇到這樣的問題在app中使用uni-forms表單,并添加校驗(yàn)規(guī)則,問題是即使輸入內(nèi)容,表單校驗(yàn)依然失敗,本文給大家分享uni-app中vue3表單校驗(yàn)失敗的問題及解決方法,感興趣的朋友一起看看吧
    2023-12-12
  • Vue3中的setup執(zhí)行時(shí)機(jī)與注意點(diǎn)說明

    Vue3中的setup執(zhí)行時(shí)機(jī)與注意點(diǎn)說明

    這篇文章主要介紹了Vue3中的setup執(zhí)行時(shí)機(jī)與注意點(diǎn)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue 項(xiàng)目常用加載器及配置詳解

    vue 項(xiàng)目常用加載器及配置詳解

    本文介紹了vue 項(xiàng)目常用加載器及配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • vue2.0基于vue-cli+element-ui制作樹形treeTable

    vue2.0基于vue-cli+element-ui制作樹形treeTable

    這篇文章主要介紹了vue2.0基于vue-cli+element-ui制作樹形treeTable,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能

    Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能

    這篇文章主要為大家詳細(xì)介紹了Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue路由傳參三種基本方式詳解

    vue路由傳參三種基本方式詳解

    這篇文章主要介紹了vue路由傳參三種基本方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • vue項(xiàng)目中chunk-vendors.js提示報(bào)錯(cuò)的查看方法(最新推薦)

    vue項(xiàng)目中chunk-vendors.js提示報(bào)錯(cuò)的查看方法(最新推薦)

    在vue項(xiàng)目中,chunk-vendors.js報(bào)出的錯(cuò)誤提示經(jīng)常會(huì)導(dǎo)致開發(fā)者困惑,正確查看錯(cuò)誤的方法是從錯(cuò)誤提示的詳細(xì)信息中找到報(bào)錯(cuò)原因,下面給大家分享vue項(xiàng)目中chunk-vendors.js提示報(bào)錯(cuò)的查看方法,感興趣的朋友一起看看吧
    2024-12-12
  • vue中ref和reactive的區(qū)別及說明

    vue中ref和reactive的區(qū)別及說明

    這篇文章主要介紹了vue中ref和reactive的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評(píng)論