最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基礎(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。雖然對于單個(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的核心對象
//定義一個(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對象
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)行修改
    // 請求頭信息
    headers: {
      //默認(rèn)的請求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,  //請求體
        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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue?Element?UI擴(kuò)展內(nèi)容過長使用tooltip顯示

    vue?Element?UI擴(kuò)展內(nèi)容過長使用tooltip顯示

    這篇文章主要為大家介紹了vue?Element?UI擴(kuò)展內(nèi)容過長使用tooltip展示鼠標(biāo)hover時(shí)的提示信息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 通過debug搞清楚.vue文件如何變成.js文件(案例詳解)

    通過debug搞清楚.vue文件如何變成.js文件(案例詳解)

    這篇文章主要介紹了通過debug搞清楚.vue文件如何變成.js文件,本文以@vitejs/plugin-vue舉例,通過debug的方式帶你一步一步的搞清楚vue文件是如何編譯為js文件的,需要的朋友可以參考下
    2024-07-07
  • vue?extend+promise封裝全局彈窗組件

    vue?extend+promise封裝全局彈窗組件

    這篇文章主要為大家詳細(xì)介紹了vue?extend+promise封裝全局彈窗組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 詳解vue-router傳參的兩種方式

    詳解vue-router傳參的兩種方式

    Vue Router 是 Vue.js 官方的路由管理器。這篇文章主要介紹了詳解vue-router傳參的兩種方式,需要的朋友可以參考下
    2018-09-09
  • 自定義elementui上傳文件以及攜帶參數(shù)問題

    自定義elementui上傳文件以及攜帶參數(shù)問題

    這篇文章主要介紹了自定義elementui上傳文件以及攜帶參數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 解決vue3中使用echart報(bào)錯(cuò):Cannot read properties of undefined (reading ‘type‘)

    解決vue3中使用echart報(bào)錯(cuò):Cannot read properties of&n

    在Vue項(xiàng)目中使用Echarts進(jìn)行數(shù)據(jù)可視化是非常常見的需求,然而有時(shí)候在引入Echarts的過程中可能會(huì)遇到報(bào)錯(cuò),本文主要介紹了解決vue3中使用echart報(bào)錯(cuò):Cannot read properties of undefined (reading ‘type‘),感興趣的可以了解一下
    2024-01-01
  • vue echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)展示

    vue echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)展示

    這篇文章主要為大家詳細(xì)介紹了vue echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue2.x中$attrs的使用方法教程

    vue2.x中$attrs的使用方法教程

    正常情況下Vue推薦用props向子組件參數(shù),但是在特定場景下,使用$attrs會(huì)更方便,下面這篇文章主要給大家介紹了關(guān)于vue2.x中$attrs使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • vue修改vue項(xiàng)目運(yùn)行端口號的方法

    vue修改vue項(xiàng)目運(yùn)行端口號的方法

    本篇文章主要介紹了vue修改vue項(xiàng)目運(yùn)行端口號的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • 在Vue當(dāng)中同時(shí)配置多個(gè)路由文件的方法案例代碼

    在Vue當(dāng)中同時(shí)配置多個(gè)路由文件的方法案例代碼

    這篇文章主要介紹了在Vue當(dāng)中同時(shí)配置多個(gè)路由文件的方法,包含具體代碼,本文分步驟結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12

最新評論

江都市| 石柱| 渝北区| 陕西省| 宁国市| 东阿县| 彭水| 斗六市| 大冶市| 绩溪县| 诸城市| 瑞金市| 舟山市| 莱芜市| 祁东县| 绥棱县| 托克逊县| 泸水县| 阳西县| 名山县| 新干县| 江华| 新沂市| 尉犁县| 广水市| 驻马店市| 沭阳县| 昭通市| 石狮市| 中宁县| 西城区| 西平县| 彰化市| 崇阳县| 五台县| 广州市| 怀仁县| 广丰县| 富裕县| 虞城县| 威宁|