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

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

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

1. 準備環(huán)境

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

2. 安裝Vite

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

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

yarn create vite 項目名 --template vue

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

cd  項目名

 yarn //安裝依賴

5.安裝配置項目所需的第三方依賴

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

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)容復制粘貼。

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)建一個index.js

//1.導入createStore函數(shù)
import {createStore} from 'vuex'?
?
//2.創(chuàng)建vuex的核心對象
//定義一個狀態(tài)
const  state={
}
//state的計算屬性
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圖標

5.4.1.安裝element-plus圖標

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)建一個http目錄, 創(chuàng)建兩個文件

1.axios實例配置文件: config.js,將以下基本配置復制粘貼

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

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

import { ElLoading,ElMessage } from 'element-plus'
import axios from 'axios'
import qs from 'qs'  //把json進行序列化成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項目創(chuàng)建的文章就介紹到這了,更多相關(guān)前端vite項目創(chuàng)建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

    這篇文章主要為大家介紹了vue?Element?UI擴展內(nèi)容過長使用tooltip展示鼠標hover時的提示信息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    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封裝全局彈窗組件

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

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

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

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

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

    解決vue3中使用echart報錯:Cannot read properties of&n

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

    vue echarts實現(xiàn)柱狀圖動態(tài)展示

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

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

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

    vue修改vue項目運行端口號的方法

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

    在Vue當中同時配置多個路由文件的方法案例代碼

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

最新評論

大埔区| 嘉兴市| 泗洪县| 武功县| 镶黄旗| 平阴县| 且末县| 偃师市| 蓝山县| 防城港市| 岑巩县| 富平县| 宜川县| 周至县| 日土县| 区。| 神木县| 酒泉市| 玉溪市| 马尔康县| 招远市| 积石山| 额敏县| 郎溪县| 阿尔山市| 新巴尔虎左旗| 资溪县| 香格里拉县| 长宁县| 诸暨市| 叙永县| 大同县| 成都市| 台南县| 咸宁市| 广灵县| 临颍县| 阿坝县| 罗城| 荆州市| 扶余县|