vue3+vite+windicss+element-plus 實(shí)用筆記,實(shí)現(xiàn)快速初始化項(xiàng)目

  • 安裝vite

    npm create vite@latest 項(xiàng)目名稱
    // 選擇 vue -> javasrcipt -> 結(jié)束
    
  • 安裝配置scss

     1掉蔬、 下載 npm i sass -S
     2再菊、 配置
      // vite.config.js
      export default defineConfig({
         css: {
            preprocessorOptions: {
              scss: {
                additionalData:'@import "./src/styles/mixins.scss";@import "./src/styles/variables.scss";' // 此處導(dǎo)入全局css樣式摹闽,也可以放在app.vue文件上引入
              }
            }
          },
      })
    
  • 安裝配置unplugin-auto-import 插件

    unplugin-auto-import 插件 免除掉繁瑣的import

    1股耽、下載
        npm i unplugin-auto-import -D 
    2根盒、配置
        // vite.config.js
        import AutoImport from 'unplugin-auto-import/vite'
        plugins: [
            vue(),
            AutoImport({
                imports: [
                    'vue',
                    'vue-router'
                ]
            })
        ],
    
  • 配置@根目錄

      1、下載
        npm install path --save
      2物蝙、配置
        // vite.config.js
        import path from 'path';
        const srcPath = path.resolve(__dirname, 'src');
        // 配置@目錄
        export default defineConfig({
          resolve: {
              alias: [
                  { find: '@', replacement: srcPath },
              ],
          },
        })
    
  • 安裝配置axios

    1炎滞、下載  
        npm install axios -S
      2、初始化
        // utils/request.js
          import axios from "axios";
      
          const service = axios.create({
          baseURL : import.meta.env.VITE_BASEURL, // 當(dāng)前環(huán)境變量
          timeout:30000,
          withCredentials:true,
          headers:{
          'Content-Type':'application/json',
          'Authorization': import.meta.env.VITE_TOKEN,
          }
          })
          
          // 添加請(qǐng)求攔截器
          service.interceptors.request.use(function (config) {
          // 在發(fā)送請(qǐng)求之前做些什么
          console.log('config',config)
          return config;
          }, function (error) {
          // 對(duì)請(qǐng)求錯(cuò)誤做些什么
          return Promise.reject(error);
          });
          
          // 添加響應(yīng)攔截器
          service.interceptors.response.use(function (response) {
          // 2xx 范圍內(nèi)的狀態(tài)碼都會(huì)觸發(fā)該函數(shù)茬末。
          // 對(duì)響應(yīng)數(shù)據(jù)做點(diǎn)什么
          return response.data;
          }, function (error) {
          // 超出 2xx 范圍的狀態(tài)碼都會(huì)觸發(fā)該函數(shù)厂榛。
          // 對(duì)響應(yīng)錯(cuò)誤做點(diǎn)什么
          return Promise.reject(error);
          });
          export default service
      3盖矫、api
         // api/index.js
          import request from "@/utils/request.js";
          export function search(params){
            return request({
              url:'/v1/search',
              method:'GET',
              params,
            })
          }
      4丽惭、使用
        // home.vue
          import {search} from '@/api/index.js'
          search().then(……)
    
  • 安裝配置pinia

      1、下載   
      npm install pinia -S
      2辈双、配置
        // main.js
        import { createPinia } from 'pinia'
        app.use(createPinia())
      3责掏、初始化(單模塊)
        // store/index.js
       import { defineStore } from 'pinia'
        const indexStore = defineStore('index',{
      state: ()=>{
          return {
              conut:1
          }
      },
      getters:{},
      actions:{},
      })
      export default indexStore  
      4、引用
        // home.vue
        import indexStore from '@/store/index.js'
        const indexstore = indexStore()
      console.log(indexstore.conut) // 1
    
  • 安裝配置router

      1湃望、下載
           npm install vue-router@4
        2换衬、配置
        // main.js
        import router  from './router/router.js'
        app.use(router)
        // router/index.js
        import { createRouter,createWebHistory} from "vue-router";
        // 路由信息
        const routes = [
            {
                path:'/',
                name:'home',
                component: () => import('@/views/home.vue')
            },
            {
                path:'/index',
                name:'index',
                component: () => import('@/views/index.vue')
            }
        ];
    
        // 導(dǎo)出路由
        const router = createRouter({
            history: createWebHistory(),
            routes
        });
    
        export default router
    
  • 安裝配置element-ui-plus(按需導(dǎo)入)

    element-ui-plus官網(wǎng)

    1、 安裝插件
      npm install -D unplugin-vue-components unplugin-auto-import
    2证芭、配置
      // vite.config.ts
      import { defineConfig } from 'vite'
        import AutoImport from 'unplugin-auto-import/vite'
        import Components from 'unplugin-vue-components/vite'
        import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
      
      export default defineConfig({
          // ...
          plugins: [
            // ...
            AutoImport({
              resolvers: [ElementPlusResolver()],
            }),
            Components({
              resolvers: [ElementPlusResolver()],
            }),
          ],
        })
    3瞳浦、手動(dòng)導(dǎo)入
      // app.vue
      <template>
          <el-button>I am ElButton</el-button>
        </template>
        <script>
          import { ElButton } from 'element-plus'
          export default {
            components: { ElButton },
          }
        </script>
    
    4、 配置icon  
        // main.js
        import * as ElementPlusIconsVue from '@element-plus/icons-vue'
        
        for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
            app.component(key, component)
        }
    5废士、配置message (不配置這個(gè)會(huì)出現(xiàn)叫潦,提示窗樣式出錯(cuò)的問題)
        import 'element-plus/theme-chalk/el-loading.css';
        import 'element-plus/theme-chalk/el-message.css';
    
  • 安裝配置element-ui-plus(全局導(dǎo)入)

      1、下載
        npm install element-plus --save
      2官硝、配置
      // main.js
        import ElementPlus from 'element-plus'
        import 'element-plus/dist/index.css'
        import * as ElementPlusIconsVue from '@element-plus/icons-vue'
    
        import 'element-plus/theme-chalk/el-loading.css';
        import 'element-plus/theme-chalk/el-message.css';
      const app = createApp(App)
      for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
          app.component(key, component)
      }
        app.use(ElementPlus)
    
  • 安裝windiCss 個(gè)人認(rèn)為挺好用的矗蕊,省了寫樣式的時(shí)間

    windicss

     1、下載
        npm i -D vite-plugin-windicss windicss
      2氢架、配置 
        // vite.config.js 
          import WindiCSS from 'vite-plugin-windicss'
    
          export default {
            plugins: [
              WindiCSS(),
            ],
          }
        // main.js
          import 'virtual:windi.css'
    
    
  • 安裝vueeuse vue提高的一個(gè)函數(shù)庫(kù)

    vueeuse

    1傻咖、安裝
      npm i @vueuse/core
    2、使用-示例
      // app.vue
      <template>
          <div ref="target"></div>
      </template>
      import { onClickOutside } from '@vueuse/core' // 監(jiān)聽點(diǎn)擊位置是否是元素以外
      const target = ref(null)
      // 切換顯示隱藏
        onClickOutside(target, (event) => {
            emits("onClear")
        })
    
    

(以上是我覺得初始化有用的插件岖研,歡迎評(píng)論補(bǔ)充嘻嘻)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末卿操,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌害淤,老刑警劉巖解滓,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異筝家,居然都是意外死亡洼裤,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門溪王,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)腮鞍,“玉大人,你說(shuō)我怎么就攤上這事莹菱∫乒” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵道伟,是天一觀的道長(zhǎng)迹缀。 經(jīng)常有香客問我,道長(zhǎng)蜜徽,這世上最難降的妖魔是什么祝懂? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮拘鞋,結(jié)果婚禮上砚蓬,老公的妹妹穿的比我還像新娘。我一直安慰自己盆色,他們只是感情好灰蛙,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著隔躲,像睡著了一般摩梧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上宣旱,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天仅父,我揣著相機(jī)與錄音,去河邊找鬼响鹃。 笑死驾霜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的买置。 我是一名探鬼主播粪糙,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼忿项!你這毒婦竟也來(lái)了蓉冈?” 一聲冷哼從身側(cè)響起城舞,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎寞酿,沒想到半個(gè)月后家夺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡伐弹,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年拉馋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惨好。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡煌茴,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出日川,到底是詐尸還是另有隱情蔓腐,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布龄句,位于F島的核電站回论,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏分歇。R本人自食惡果不足惜傀蓉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望卿樱。 院中可真熱鬧僚害,春花似錦、人聲如沸繁调。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蹄胰。三九已至,卻和暖如春奕翔,著一層夾襖步出監(jiān)牢的瞬間裕寨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工派继, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留宾袜,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓驾窟,卻偏偏與公主長(zhǎng)得像庆猫,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子绅络,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

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