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

一文帶你從零開始搭建vue3項(xiàng)目

 更新時(shí)間:2022年06月26日 10:12:06   作者:KONG  
使用VUE3開發(fā)很久了,但一直沒進(jìn)行總結(jié)和記錄,下面這篇文章主要給大家介紹了關(guān)于從零開始搭建vue3項(xiàng)目的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下

說明

記錄一次Vue3的項(xiàng)目搭建過程。文章基于 vue3.2.6 和 vite2.51 版本,使用了ui庫 Element plus,vue-router4,Layout布局封裝,axios請求封裝,別名配置等。

開始

1. 使用 vscode 開發(fā)工具安裝vue3的插件 Volar ,在vue2中我們使用的是Vetur。

2. 執(zhí)行初始化及安裝命令:

npm init vite 初始化vite此過程可以輸入項(xiàng)目名、選擇vue/react項(xiàng)目及js/ts環(huán)境選擇,vue3已經(jīng)完全支持ts,此文章使用的是js。npm install 安裝依賴。最后執(zhí)行npm run dev運(yùn)行項(xiàng)目。

運(yùn)行過程時(shí)如果出現(xiàn)上圖的報(bào)錯(cuò)信息,可以手動(dòng)執(zhí)行 node node_modules/esbuild/install.js,然后再執(zhí)行npm run dev

3. 安裝vue-router

執(zhí)行 npm install vue-router@4 , vue3對應(yīng)的vue-router和vuex的版本都是 4.0。執(zhí)行命令安裝完成之后,在目錄下創(chuàng)建 src/router/index.js 寫入下面的配置:

import { createRouter, createWebHistory } from 'vue-router'
const routes = [
// ...
]

export default createRouter({
history: createWebHistory(),
routes,
})

main.js中使用

// ...+
import router from './router/index'
createApp(App).use(router).mount('#app')

vue-router4寫法和以前的有些區(qū)別 hash模式 createWebHashHistory history模式 createWebHistory,具體可查看官網(wǎng)。

4. 全局樣式及sass安裝(使用@路徑需要配置別名,后文有相應(yīng)的說明)

執(zhí)行命令npm i sass -D,然后在目錄下創(chuàng)建 src/styles/index.scss:

// @import './a.scss'; 
// 作為出口組織這些樣式文件,同時(shí)編寫一些全局樣式

在 mian.js 中引入

import '@/styles/index.scss'

tips: vue3中樣式穿透 使用::deep(.className) 或者 deep(.className)

5. Element plus按需引入和全局引入

執(zhí)行npm i element3 -S命令安裝,如果你能用到里面的大多數(shù)組件,就用全局引入方式,如下:

// main.js
import element3 from "element3";
import "element3/lib/theme-chalk/index.css";
createApp(App).use(router).use(element3).mount('#app')

如果你只用到幾個(gè)組件,就可以按需加載優(yōu)化性能,創(chuàng)建src/plugins/element3.js,如下

// 按需引入 plugins/element3.js
import { ElButton, ElMenu, ElMenuItem } from 'element3'
import 'element3/lib/theme-chalk/button.css'
import 'element3/lib/theme-chalk/menu.css'
import 'element3/lib/theme-chalk/menu-item.css'
export default function (app) {  
    app.use(ElButton) 
    app.use(ElMenu) 
    app.use(ElMenuItem)
}
// main.js中引用
import element3 from '@/plugins/element3.js'
createApp(App).use(router).use(element3).mount('#app')

6. Layout布局,創(chuàng)建文件src/layout/index.vue

// src/layout/index.vue
<template>
   <!-- 頂部導(dǎo)航 -->
  <Navbar />
  <!-- 頁面內(nèi)容部分、路由出口 -->
  <AppMain />
  <!-- 底部內(nèi)容 -->
  <Footer />
</template>

<script setup>
import Navbar from './Navbar.vue'
import AppMain from './AppMain.vue'
import Footer from './Footer.vue'
</script>

根據(jù)自己的需求設(shè)計(jì)布局,使用Layout布局時(shí),需要注意將Layout.vue作為父路由,路由設(shè)計(jì)大概像下面這樣:

// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
import Home from '@/views/home/Home.vue'
import Test from '@/views/test/Test.vue'
const routes = [
  {
    path: '/',
    component: Layout,
    children: [{ path: '', component: Home }],
  },
  {
    path: '/test',
    component: Layout,
    children: [{ path: '', component: Test }],
  },
]

export default createRouter({
  history: createWebHistory(),
  routes,
})

7. axios請求封裝

執(zhí)行命令 npm i axios 安裝axios

新建 src/utils/request.js,在此文件中進(jìn)行封裝axios

import axios from 'axios'
// 可以導(dǎo)入element plus 的彈出框代替alert進(jìn)行交互操作

// create an axios instance
const service = axios.create({
  baseURL: import.meta.env.VITE_APP_BASEURL, // 使用設(shè)置好的全局環(huán)境
  timeout: 30 * 1000, // request timeout
})

// request interceptor
service.interceptors.request.use(
  (config) => {
    // 此處可以執(zhí)行處理添加token等邏輯
    // config.headers["Authorization"] = getToken();
    return config
  },
  (error) => {
    console.log(error)
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  (response) => {
    const res = response.data // 根據(jù)接口返回參數(shù)自行處理

    if (res.code !== 200) {
      if (res.code === 50000) {
        // 根據(jù)狀態(tài)碼自行處理
        alert('服務(wù)器內(nèi)部出現(xiàn)異常,請稍后再試')
      }
      return Promise.reject(new Error(res.msg || 'Error'))
    } else {
      // 調(diào)用成功返回?cái)?shù)據(jù)
      return Promise.resolve(res)
    }
  },
  (error) => {
    console.log('err' + error) // 出現(xiàn)異常的處理
    return Promise.reject(error)
  }
)

export default service

新建 src/api 目錄,可以每個(gè)模塊或每個(gè)頁面單獨(dú)建立一個(gè)js文件,方便管理維護(hù)api。此處示例,新建 src/api/home.js 文件,寫入代碼

// 引入封裝好的 request.js
import request from '@/utils/request'

export function getList(query) {
  return request({
    url: '/list',
    method: 'get',
    params: query,
  })
}

在 home.vue 中使用

<script setup>
import { getList } from '@/api/home.js'
const query = { pagenum: 1 }
getList(query)
  .then((res) => {
    console.log(res) // 調(diào)用成功返回的數(shù)據(jù)
  })
  .error((err) => {
    console.log(err) // 調(diào)用失敗要執(zhí)行的邏輯
  })
</script>

8. 環(huán)境變量相關(guān)

項(xiàng)目根目錄下創(chuàng)建三個(gè)文件.env.production 生產(chǎn)環(huán)境 .env.development 開發(fā)環(huán)境 .env.staging 測試環(huán)境 ,分別加入下面的代碼,在不同的編譯環(huán)境下,打包時(shí)自動(dòng)執(zhí)行當(dāng)前環(huán)境下的代碼

# .env.production
VITE_APP_BASEURL=https://www.prod.api/
# .env.development
VITE_APP_BASEURL=https://www.test.api/
# .env.staging
VITE_APP_BASEURL=https://www.test.api/

使用:

console.log(import.meta.env.VITE_APP_BASEURL)
// 在不同編譯環(huán)境下控制臺會輸出不同的url路徑

package.json中通過傳遞 --mode 選項(xiàng)標(biāo)志來覆蓋命令使用的默認(rèn)模式

  "scripts": {
    "dev": "vite",
    "build:stage": "vite build --mode staging",
    "build:prod": "vite build --mode production",
    "serve": "vite preview"
  },

這樣,生產(chǎn)環(huán)境打包執(zhí)行npm run build:prod,測試/預(yù)發(fā)布環(huán)境打包npm run build:stage

9. vite中別名配置

根目錄下 vite.config.js 文件添加代碼

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: [{ find: '@', replacement: resolve(__dirname, 'src') }],
  },
  base: './',
})

更多配置項(xiàng)查看官網(wǎng)

總結(jié)

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

相關(guān)文章

  • Vue中Class和Style實(shí)現(xiàn)v-bind綁定的幾種用法

    Vue中Class和Style實(shí)現(xiàn)v-bind綁定的幾種用法

    項(xiàng)目開發(fā)中給元素添加/刪除 class 是非常常見的行為之一, 例如網(wǎng)站導(dǎo)航都會給選中項(xiàng)添加一個(gè) active 類用來區(qū)別選與未選中的樣式,那么在 vue 中 我們?nèi)绾翁幚磉@類的效果呢?下面我們就一起來了解一下
    2021-05-05
  • vue部署到線上為啥會出現(xiàn)404的原因分析及解決

    vue部署到線上為啥會出現(xiàn)404的原因分析及解決

    這篇文章主要介紹了vue部署到線上為啥會出現(xiàn)404的原因分析及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 解決vue的過渡動(dòng)畫無法正常實(shí)現(xiàn)問題

    解決vue的過渡動(dòng)畫無法正常實(shí)現(xiàn)問題

    今天小編就為大家分享一篇解決vue的過渡動(dòng)畫無法正常實(shí)現(xiàn)問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • Vue調(diào)試工具vue-devtools的安裝與使用

    Vue調(diào)試工具vue-devtools的安裝與使用

    vue-devtools是專門調(diào)試vue項(xiàng)目的調(diào)試工具,安裝成功之后,右邊會出現(xiàn)一個(gè)vue,就可以在線可以調(diào)試vue了,下面這篇文章主要給大家介紹了關(guān)于Vue調(diào)試工具vue-devtools的安裝與使用的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • vue實(shí)現(xiàn)web在線聊天功能

    vue實(shí)現(xiàn)web在線聊天功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)web在線聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • 解決vue更新路由router-view復(fù)用組件內(nèi)容不刷新的問題

    解決vue更新路由router-view復(fù)用組件內(nèi)容不刷新的問題

    今天小編就為大家分享一篇解決vue更新路由router-view復(fù)用組件內(nèi)容不刷新的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Element-Plus實(shí)現(xiàn)動(dòng)態(tài)渲染圖標(biāo)的示例代碼

    Element-Plus實(shí)現(xiàn)動(dòng)態(tài)渲染圖標(biāo)的示例代碼

    在Element-Plus中,我們可以使用component標(biāo)簽來動(dòng)態(tài)渲染組件,本文主要介紹了Element-Plus?實(shí)現(xiàn)動(dòng)態(tài)渲染圖標(biāo)教程,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • 詳解如何寫出一個(gè)利于擴(kuò)展的vue路由配置

    詳解如何寫出一個(gè)利于擴(kuò)展的vue路由配置

    這篇文章主要介紹了詳解如何寫出一個(gè)利于擴(kuò)展的vue路由配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05
  • Vue通過v-for實(shí)現(xiàn)年份自動(dòng)遞增

    Vue通過v-for實(shí)現(xiàn)年份自動(dòng)遞增

    這篇文章主要為大家詳細(xì)介紹了Vue通過v-for實(shí)現(xiàn)年份自動(dòng)遞增,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • vue.js實(shí)例todoList項(xiàng)目

    vue.js實(shí)例todoList項(xiàng)目

    本篇文章主要介紹了vue.js實(shí)例todoList項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07

最新評論

白山市| 平定县| 山阴县| 乌鲁木齐市| 堆龙德庆县| 二连浩特市| 台中市| 兴海县| 保亭| 迁西县| 北碚区| 闽侯县| 石台县| 三河市| 威信县| 靖边县| 平原县| 北票市| 黄龙县| 澄迈县| 庐江县| 时尚| 上林县| 高阳县| 营山县| 葫芦岛市| 准格尔旗| 西乡县| 奈曼旗| 宣汉县| 八宿县| 苏州市| 赤水市| 文化| 方正县| 德格县| 静安区| 海口市| 冀州市| 北票市| 将乐县|