vite搭建vue3+ant Design項(xiàng)目(Vue Router+Vuex+axios)

  1. vite 創(chuàng)建項(xiàng)目點(diǎn)擊查看
  2. 配置vite.config.js文件(此步驟暫時有問題爱榕,跳過驰后,系統(tǒng)創(chuàng)建文件暫時不更改)
  • 在項(xiàng)目根目錄創(chuàng)建vite.config.js文件
  • 寫入基本配置
const path = require('path')
module.exports = {
    base:"./",//公共基礎(chǔ)路徑 打包路徑
    resolve: {
        alias:{
            '@': path.resolve(__dirname, './src')//別名設(shè)置
        }
    },
    // host: 'localhost',
    port:8888,//啟動端口
    // https: true, // 開啟https
    open: true, // 自動開啟窗口
    proxy: { // 代理配置
        '/api': {
            target: 'http://xxx.com',//后端服務(wù)地址
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, '') // 重寫路徑
        }
    },
}
  1. 安裝ant-design-vue組件庫(可能報(bào)錯靖榕,報(bào)錯后再重裝乳规,后需要重新安裝依賴
npm i --save ant-design-vue@next
  1. 在main.js中完整引入組件:
import { createApp } from 'vue'
import App from './App.vue'
//完整引入組件锤岸,僅做測試用
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
 
const app = createApp(App);
app.use(Antd);
app.mount('#app')
  1. 在App.vue中添加按鈕,測試是否引入成功:
<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <HelloWorld msg="Hello Vue 3 + Vite" />
  <a-button type="primary">Primary</a-button>
  <a-button>Default</a-button>
  <a-button type="dashed">Dashed</a-button>
  <a-button type="danger">Danger</a-button>
  <a-config-provider :auto-insert-space-in-button="false">
    <a-button type="primary">按鈕</a-button>
  </a-config-provider>
  <a-button type="primary">按鈕</a-button>
  <a-button type="link">Link</a-button>
</template>
運(yùn)行結(jié)果
  1. 配置路由
  • 安裝 vue-router
npm install --save vue-router@next
  • src目錄下 新建router文件夾
  • 新建index.js和router.config.js文件
  • index.js寫入內(nèi)容
import { createRouter, createWebHistory} from 'vue-router';
import routes from './router.config';
 
export const router = createRouter({
    history: createWebHistory(),
 // createWebHistory => 不帶#號,需后端支持  createWebHashHistory帶#號
    routes
});
  • router.config.js寫入內(nèi)容
export default [
    {
        path: '/',
        name: 'root',
        redirect: '/root',
        component: () => import('../App.vue'),
        children:[]
    },
    {
        path: '/btn',
        name: 'btn',
        component: () => import('../views/btn.vue'),
    }
]
  • 在main.js中引入router并使用
import { createApp } from 'vue'
import App from './App.vue'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
import { router } from './router'

const app = createApp(App);
app.use(Antd);
app.use(router);
app.mount('#app')
  • src目錄下切黔,創(chuàng)建views文件夾
  • 在views文件夾下增加btn.vue文件
<template>
  <a-button type="primary">Primary</a-button>
  <a-button>Default</a-button>
  <a-button type="dashed">Dashed</a-button>
  <a-button type="danger">Danger</a-button>
  <a-config-provider :auto-insert-space-in-button="false">
    <a-button type="primary">按鈕</a-button>
  </a-config-provider>
  <a-button type="primary">按鈕</a-button>
  <a-button type="link">Link</a-button>
</template>
  • 修改App.vue文件
<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <HelloWorld msg="Hello Vue 3 + Vite" />
  <router-link to='/btn'> btn </router-link> //路由
  <router-view />
</template>
  • 運(yùn)行項(xiàng)目測試
    此時點(diǎn)擊btn可以正常跳轉(zhuǎn)


    運(yùn)行結(jié)果
  • 測試路由是否正常


    顯示正常
  1. 使用Vuex
  • 安裝vuex
npm install vuex@next --save
  • src目錄下砸脊,新建store文件夾
  • 在store文件夾下創(chuàng)建index.js,寫入內(nèi)容
import { createStore } from "vuex";
 
export default createStore({
    state: {
        demo:'vuex'
    },
    getters:{},
    mutations: {},
    actions: {},
    modules: {}
});
  • 在main.js中引入并使用
import { createApp } from 'vue'
import App from './App.vue'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
import { router } from './router'
import store from "./store";

const app = createApp(App);
app.use(Antd);
app.use(router);
app.use(store);
app.mount('#app')
  • 在App.vue中加入代碼測試
<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <HelloWorld msg="Hello Vue 3 + Vite" />
  <h1>{{this.$store.state.demo}}</h1>  //vuex
  <router-link to='/btn'> btn </router-link>
  <router-view />
</template>
  • 保存并重新運(yùn)行


    vuex正常
  1. 使用axios
  • 安裝axios
npm install axios --save
  • 配置axios
  • src目錄下,創(chuàng)建http文件夾
  • 在http文件夾下創(chuàng)建index.js纬霞、axios.config.js凌埂、axios.interceptors.config.js、api.js文件
    index.js寫入代碼
import axios from 'axios';
import config from './axios.config';
import {setInterceptors} from './axios.interceptors.config';

const axiosInstance = axios.create(config);

setInterceptors(axiosInstance);

export default axiosInstance

axios.config.js配置文件中寫入代碼

export default {
    baseURL: '/api', // 服務(wù)器地址或配置的代理的路徑
    timeout: 10000,  // 超時時間
    headers: {} // 配置請求頭
}

axios.interceptors.config.js攔截器文件中寫入代碼

export const setInterceptors = (axiosInstance) => {
    // 請求攔截
    axiosInstance.interceptors.request.use(request => {
        return request;
    }, error => Promise.reject(error));

    // 響應(yīng)攔截
    axiosInstance.interceptors.response.use(response => {
        return response;
    }, error => {
        return Promise.reject(error.response);
    })
};

api.js文件中加入你要請求的接口

import axiosInstance from './index'

// 請求接口封裝
export const axiosGet = (params) => axiosInstance.post('/demo', params);

axios已經(jīng)完全配置好了诗芜,在需要使用請求的地方直接從api.js文件中導(dǎo)出瞳抓。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市伏恐,隨后出現(xiàn)的幾起案子孩哑,更是在濱河造成了極大的恐慌,老刑警劉巖翠桦,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件横蜒,死亡現(xiàn)場離奇詭異,居然都是意外死亡销凑,警方通過查閱死者的電腦和手機(jī)丛晌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來斗幼,“玉大人澎蛛,你說我怎么就攤上這事⊥闪” “怎么了谋逻?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長渠羞。 經(jīng)常有香客問我斤贰,道長,這世上最難降的妖魔是什么次询? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任荧恍,我火速辦了婚禮,結(jié)果婚禮上屯吊,老公的妹妹穿的比我還像新娘送巡。我一直安慰自己,他們只是感情好盒卸,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布骗爆。 她就那樣靜靜地躺著,像睡著了一般蔽介。 火紅的嫁衣襯著肌膚如雪摘投。 梳的紋絲不亂的頭發(fā)上煮寡,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天,我揣著相機(jī)與錄音犀呼,去河邊找鬼幸撕。 笑死,一個胖子當(dāng)著我的面吹牛外臂,可吹牛的內(nèi)容都是我干的坐儿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼宋光,長吁一口氣:“原來是場噩夢啊……” “哼貌矿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起罪佳,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤逛漫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后菇民,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體尽楔,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年第练,在試婚紗的時候發(fā)現(xiàn)自己被綠了阔馋。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡娇掏,死狀恐怖呕寝,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情婴梧,我是刑警寧澤下梢,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站塞蹭,受9級特大地震影響孽江,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜番电,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一岗屏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧漱办,春花似錦这刷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至洞辣,卻和暖如春咐刨,著一層夾襖步出監(jiān)牢的瞬間昙衅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工定鸟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留绒尊,地道東北人。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓仔粥,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蟹但。 傳聞我的和親對象是個殘疾皇子躯泰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內(nèi)容