国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vite + vue3 + vue-router4 + ts + element plus + pinia + axios構(gòu)建項目

這篇具有很好參考價值的文章主要介紹了vite + vue3 + vue-router4 + ts + element plus + pinia + axios構(gòu)建項目。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

最后是完整的vite.config.ts、main.ts配置

1、先用vite創(chuàng)建一個項目

npm create vite@latest

2、安裝element plus

yarn add element-plus @element-plus/icons-vue

vite.config.ts配置組件按需導(dǎo)入,圖標自動導(dǎo)入

npm install -D unplugin-vue-components unplugin-auto-import unplugin-icons

?

import path from 'path'
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
// @ts-ignore
import Icons from 'unplugin-icons/vite'
// @ts-ignore
import IconsResolver from 'unplugin-icons/resolver'
// @ts-ignore
const pathSrc = path.resolve(__dirname, 'src/types')

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        AutoImport({
            // Auto import functions from Vue, e.g. ref, reactive, toRef...
            // 自動導(dǎo)入 Vue 相關(guān)函數(shù),如:ref, reactive, toRef 等
            imports: ['vue'],

            // Auto import functions from Element Plus, e.g. ElMessage, ElMessageBox... (with style)
            // 自動導(dǎo)入 Element Plus 相關(guān)函數(shù),如:ElMessage, ElMessageBox... (帶樣式)
            resolvers: [
                ElementPlusResolver(),

                // Auto import icon components
                // 自動導(dǎo)入圖標組件
                IconsResolver({
                    prefix: 'Icon',
                }),
            ],

            dts: path.resolve(pathSrc, 'auto-imports.d.ts'),
        }),
        Components({
            resolvers: [
                // Auto register icon components
                // 自動注冊圖標組件
                //{prefix}-{collection}-{icon},
                // By default, the prefix is set to i while you can customize via config
                // 'ep'是element plus 圖標的標識(固定的)https://icon-sets.iconify.design/
                IconsResolver({
                    enabledCollections: ['ep'],
                }),
                // Auto register Element Plus components
                // 自動導(dǎo)入 Element Plus 組件
                ElementPlusResolver(),
            ],

            dts: path.resolve(pathSrc, 'components.d.ts'),
        }),
        Icons({
            autoInstall: true,
        }),

    ],
})

main.ts配置

/**ElementPlus*/
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

$vueApp.use(ElementPlus, {size: 'small', zIndex: 100});

頁面文件使用

<i-ep-edit/>
<el-button type="primary">
     <i-ep-edit slot="icon"/>
     按鈕
</el-button>

<el-button type="primary" @click="showMessage">showMessage</el-button>
<script setup lang="ts">

const {proxy} = getCurrentInstance() as any;

const showMessage = () => {
    proxy.$message.success({
        message: 'proxy: ',
    })
}
</script>

3、安裝axios

yarn add axios

main.ts

/**axios*/
import axios from "@/plugins/axios";//配置封裝

$vueApp.config.globalProperties.$axios = axios;

4、安裝pinia

yarn add pinia

/stores/index.ts

import { createPinia} from 'pinia'
const pinia = createPinia()

export default pinia;

/stores/counter.ts

import {defineStore} from "pinia";
export const useCounterStore = defineStore('counter', {
    state: () => ({ count: 0 }),
    getters: {
        double: (state) => state.count * 2,
    },
    actions: {
        increment() {
            this.count++
        },
    },
})

main.ts

/**pinia*/
import pinia from './stores/index';

$vueApp.use(pinia)

頁面使用

 <div>
            count:{{ count }},double:{{ double }}
            <el-button type="primary" @click="increment">increment</el-button>
</div>
import {storeToRefs} from 'pinia'
import {useCounterStore} from '@/stores/counter'
// 可以在組件中的任意位置訪問 `store` 變量 ?
const store = useCounterStore()
// `name` 和 `doubleCount` 是響應(yīng)式的 ref
// 同時通過插件添加的屬性也會被提取為 ref
// 并且會跳過所有的 action 或非響應(yīng)式 (不是 ref 或 reactive) 的屬性
const {count, double} = storeToRefs(store)
// 作為 action 的 increment 可以直接解構(gòu)
const {increment} = store;

5、安裝router4

yarn add vue-router

/router/index

import * as VueRouter from 'vue-router'

// 1. 定義路由組件.
// 也可以從其他文件導(dǎo)入
const Home = { template: '<div>Home1111</div>' }

// 2. 定義一些路由
// 每個路由都需要映射到一個組件。
// 我們后面再討論嵌套路由。
const routes = [
    { path: '/', component: Home },
    { path: '/about', component:  () => import('@/views/about.vue') },
    { path: '/test', component: () => import('@/views/test.vue') },
]

// 3. 創(chuàng)建路由實例并傳遞 `routes` 配置
// 你可以在這里輸入更多的配置,但我們在這里
// 暫時保持簡單
const router = VueRouter.createRouter({
    history: VueRouter.createWebHistory(),
    routes, // `routes: routes` 的縮寫
})
export default router;

main.ts

/**路由*/
import router from '@/router/index';
$vueApp.use(router)

?

6、vite.config.ts常用配置@文件路徑、css預(yù)處理、代理服務(wù)https(需要證書,否則會報錯)

npm install -D @vitejs/plugin-basic-ssl
import path from 'path'
import {defineConfig} from 'vite'
import basicSsl from '@vitejs/plugin-basic-ssl'


// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        //https證書
        basicSsl(),
    ],
    resolve: {
        alias: [
            {
                find: '@',
                // @ts-ignore
                replacement: path.resolve(__dirname, 'src'),
            }
        ],
    },
    // css預(yù)處理器
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@import "@/style/constant.scss";`,//變量文件,在其他頁面中可以直接使用變量,不用每次引入
            },
        },
    },
    server: {
        https: true,
        host: '0.0.0.0',
        port: 8080,
        proxy: {
            '/api': {
                target: 'https://*****/api',//local環(huán)境
                changeOrigin: true, //允許跨域
                rewrite: (path) => path.replace(/^\/api/, '/'),
            },
        }
    }
})

tsconfig.json文件加上如下配置,否則@引入文件會報錯

{
  "compilerOptions": {
    ......
    "baseUrl": "./",
    "paths": {
      "@/*":["src/*"],
    }
  },

}

7、transition過度效果,和vue2區(qū)別是class名稱變了

<router-view v-slot="{ Component }">
            <transition name="fade-transform">
                <component :is="Component" />
            </transition>
        </router-view>
/* fade-transform */
.fade-transform-leave-active,
.fade-transform-enter-active {
  transition: all .2s;
}

.fade-transform-enter-from {
  opacity: 0;
  transform: translateX(-30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

?

8、自定義指令

yarn add throttle-debounce

/directive/clickDebounce

import {on, off} from '@/utils/dom'


let fn = null, $vueApp = null;
const debounceDirective = {
    name: 'debounce',
    install(Vue, {defaultTime = 300} = {}) {
        $vueApp.directive('debounce', {
            beforeMount(el, {value, arg}) {
                fn = debounce(arg || defaultTime, value)
                on(el, 'click', fn)
            },

            unMounted: function (el) {
                if (fn) {
                    off(el, 'click', fn)
                }
            },
        })
    },
}
export default (vueApp) => {
    $vueApp = vueApp;
    $vueApp.use(debounceDirective)
}

main.ts

/**指令*/
import debounceDirective from '@/directive/clickDebounce/index.js'

debounceDirective($vueApp);

9、過濾器

/filters/index

const getTime = (time, type = 1) => {
  return 'getTime'
}
export default{
  getTime
}

main.ts

/**過濾器*/
import filters from './filters/index.js'

Object.keys(filters).forEach(
    (k) =>
        ((
            $vueApp.config.globalProperties.$filters ||
            ($vueApp.config.globalProperties.$filters = {})
        )[k] = filters[k])
)

頁面使用

   <div>
        {{ $filters.getTime(Date.now()) }}
    </div>

10、打包

打包報錯把package.json中

"build": "vue-tsc && vite build",

改成

"build": "vite build",

總結(jié)?

import path from 'path'
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
import basicSsl from '@vitejs/plugin-basic-ssl'
// @ts-ignore
import Icons from 'unplugin-icons/vite'
// @ts-ignore
import IconsResolver from 'unplugin-icons/resolver'
// @ts-ignore
const pathSrc = path.resolve(__dirname, 'src/types')

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        AutoImport({
            // Auto import functions from Vue, e.g. ref, reactive, toRef...
            // 自動導(dǎo)入 Vue 相關(guān)函數(shù),如:ref, reactive, toRef 等
            imports: ['vue'],

            // Auto import functions from Element Plus, e.g. ElMessage, ElMessageBox... (with style)
            // 自動導(dǎo)入 Element Plus 相關(guān)函數(shù),如:ElMessage, ElMessageBox... (帶樣式)
            resolvers: [
                ElementPlusResolver(),

                // Auto import icon components
                // 自動導(dǎo)入圖標組件
                IconsResolver({
                    prefix: 'Icon',
                }),
            ],

            dts: path.resolve(pathSrc, 'auto-imports.d.ts'),
        }),
        Components({
            resolvers: [
                // Auto register icon components
                // 自動注冊圖標組件
                //{prefix}-{collection}-{icon},
                // By default, the prefix is set to i while you can customize via config
                // 'ep'是element plus 圖標的標識(固定的)https://icon-sets.iconify.design/
                IconsResolver({
                    enabledCollections: ['ep'],
                }),
                // Auto register Element Plus components
                // 自動導(dǎo)入 Element Plus 組件
                ElementPlusResolver(),
            ],

            dts: path.resolve(pathSrc, 'components.d.ts'),
        }),
        Icons({
            autoInstall: true,
        }),
        //https證書
        basicSsl(),
    ],
    resolve: {
        alias: [
            {
                find: '@',
                // @ts-ignore
                replacement: path.resolve(__dirname, 'src'),
            }
        ],
    },
    // css預(yù)處理器
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@import "@/style/constant.scss";`,
            },
        },
    },
    server: {
        https: true,
        host: '0.0.0.0',
        port: 8080,
        proxy: {
            '/api': {
                target: 'https://*******/api',
                changeOrigin: true, //允許跨域
                rewrite: (path) => path.replace(/^\/api/, '/'),
            },
        }
    }
})

?

import {createApp} from 'vue'
import '@/style/index.scss'
import App from './App.vue'

const $vueApp = createApp(App);

/**ElementPlus*/
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

$vueApp.use(ElementPlus, {size: 'small', zIndex: 100});

/**過濾器*/
import filters from './filters/index.js'

Object.keys(filters).forEach(
    (k) =>
        ((
            $vueApp.config.globalProperties.$filters ||
            ($vueApp.config.globalProperties.$filters = {})
        )[k] = filters[k])
)

/**axios*/
import axios from "@/plugins/axios";

$vueApp.config.globalProperties.$axios = axios;

/**pinia*/
import pinia from './stores/index';

$vueApp.use(pinia)

/**指令*/
import debounceDirective from '@/directive/clickDebounce/index.js'

debounceDirective($vueApp);

/**路由*/
import router from '@/router/index';
$vueApp.use(router)

$vueApp.mount('#app')

?文章來源地址http://www.zghlxwxcb.cn/news/detail-424852.html

到了這里,關(guān)于vite + vue3 + vue-router4 + ts + element plus + pinia + axios構(gòu)建項目的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 從0開始搭建一個vue3+vite+ts+pinia+element-plus的項目

    從0開始搭建一個vue3+vite+ts+pinia+element-plus的項目

    前言:vue3+ts+vite大家已經(jīng)都開始用了,最近也在學(xué)習(xí),基本上是零基礎(chǔ)開始ts的學(xué)習(xí),很多語法知識是邊寫邊查,沒有系統(tǒng)的學(xué)習(xí)ts。此處展示從零開始,搭建的一個框架,方便拿來即用! 其中框架選擇vue,語言選擇typeScript 項目啟動成功以后如下所示: 為了方便日常工作中

    2024年02月06日
    瀏覽(28)
  • vue3+vite+pinia+vue-router+ol項目創(chuàng)建及配置

    vue3+vite+pinia+vue-router+ol項目創(chuàng)建及配置

    vite官網(wǎng) 注意:兩種方式創(chuàng)建目錄結(jié)構(gòu)一致 方式一:vite創(chuàng)建腳手架命令: 命令行:npm create vite@latest 然后選擇 方式二:命令行直接聲明帶上vue 定義:pinia是一個是Vue官方團隊推薦代替Vuex的一款輕量級狀態(tài)管理庫。 pinia官網(wǎng)中文文檔 命令行:yarn add pinia 或者 npm i pinia vue-rou

    2024年02月16日
    瀏覽(125)
  • Vite4+Pinia2+vue-router4+ElmentPlus搭建Vue3項目(組件、圖標等按需引入)[保姆級]

    Vite4+Pinia2+vue-router4+ElmentPlus搭建Vue3項目(組件、圖標等按需引入)[保姆級]

    本文為快速搭建vite4項目,一些插件的詳情就不做過多的解釋,都放有官網(wǎng)鏈接,需要深入了解的小伙伴可自行查看。至于為什么選擇使用vite,因為它具備著快速啟動、按需編譯、模塊熱更新的亮點。歸根結(jié)底最大的特點就是 快 。vue的創(chuàng)始人是尤雨溪大佬,vite也是他。所以

    2023年04月19日
    瀏覽(31)
  • NodeJS+Vue+Element-Ui/Plus+Axios+Vue-router+vuex 詳細下載、安裝、創(chuàng)建項目、引入

    NodeJS+Vue+Element-Ui/Plus+Axios+Vue-router+vuex 詳細下載、安裝、創(chuàng)建項目、引入

    1.下載過程默認下一步 (1)這個是官網(wǎng)全版目錄,下載太慢(一般下載不了);但是它寫了所有nodejs和npm相互對應(yīng)的版本,可以以此為參考、防止版本不對應(yīng); NodeJS各個歷史版本下載 https://nodejs.org/zh-cn/download/releases/ (2)這里有一個快速下載地址,只有16.18.1這個版本,但這

    2023年04月18日
    瀏覽(36)
  • Vue3+Vue-Router+Element-Plus根據(jù)后端數(shù)據(jù)實現(xiàn)前端動態(tài)路由——權(quán)限管理模塊

    Vue3+Vue-Router+Element-Plus根據(jù)后端數(shù)據(jù)實現(xiàn)前端動態(tài)路由——權(quán)限管理模塊

    提示:文章內(nèi)容仔細看一些,或者直接粘貼復(fù)制,效果滿滿 提示:文章大概 1、項目:前后端分離 2、前端:基于Vite創(chuàng)建的Vue3項目 3、后端:沒有,模擬的后端數(shù)據(jù) 4、關(guān)于路徑“@”符號——vite.config.js 文件里修改 提示:以下是本篇文章正文內(nèi)容,下面案例可供復(fù)制粘貼使用

    2024年02月02日
    瀏覽(123)
  • Vue3+TS+Vite創(chuàng)建項目,并導(dǎo)入Element-plus和Sass

    Vue3+TS+Vite創(chuàng)建項目,并導(dǎo)入Element-plus和Sass

    1.桌面新建一個文件夾Vue3-app 打開編輯器導(dǎo)入文件夾,編輯器打開終端輸入或者命令行工具cd到項目目錄下輸入 npm init vue@latest 回車運行 這里我選擇了TS+Vite來開發(fā),并選擇安裝路由 2.cd到 vue-project目錄下 輸入 npm install 回車運行 3.安裝完成后 輸入 npm run dev 回車運行 瀏覽器打開

    2024年02月16日
    瀏覽(24)
  • vite+ts出現(xiàn)紅色波浪線;找不到模塊“vue-router

    提示:找不到模塊“vue-router”。你的意思是要將 \\\"moduleResolution\\\" 選項設(shè)置為 \\\"node\\\",還是要將別名添加到 \\\"paths\\\" 選項中?ts(2792) 解決方案一: 查看官方文檔:https://www.tslang.cn/docs/handbook/migrating-from-javascript.html 獲取聲明文件 如果你開始做轉(zhuǎn)換到TypeScript導(dǎo)入,你可能會遇到 Canno

    2024年02月08日
    瀏覽(21)
  • Vue3.2 + TypeScript + Pinia + Vite4 + Element-Plus + 微前端(qiankun) 后臺管理系統(tǒng)模板(已開源---顯示項目頁面截圖)

    Vue3.2 + TypeScript + Pinia + Vite4 + Element-Plus + 微前端(qiankun) 后臺管理系統(tǒng)模板(已開源---顯示項目頁面截圖)

    Wocwin-Admin,是基于 Vue3.2、TypeScript、Vite、Pinia、Element-Plus、Qiankun(微前端) 開源的一套后臺管理模板;同時集成了微前端 qiankun也可以當(dāng)做一個子應(yīng)用。項目中組件頁面使用了Element-plus 二次封裝 t-ui-plus 組件,目前已新增fastmock接口。 Link:https://wocwin.github.io/wocwin-admin/ 賬號:

    2024年02月08日
    瀏覽(40)
  • Vue3+vite搭建基礎(chǔ)架構(gòu)(6)--- 使用vue-router

    Vue3+vite搭建基礎(chǔ)架構(gòu)(6)--- 使用vue-router

    這里記錄下自己在Vue3+vite的項目使用vue-router的過程,不使用ts語法,方便以后直接使用。這里承接自己的博客Vue3+vite搭建基礎(chǔ)架構(gòu)(5)— 使用vue-i18n這篇博客,在該博客項目的基礎(chǔ)上增加使用vue-router。 Vue3使用vue-router官方文檔:https://router.vuejs.org/zh/installation.html 根據(jù)官網(wǎng)給

    2024年02月21日
    瀏覽(26)
  • 超級詳細 最新 vite4+vue3+ts+element-plus+eslint-prettier 項目搭建流程

    系列文章目錄 【element-plus】 table表格每行圓角解決方案 element也通用 【Vue3+Vite+Ts+element-plus】使用tsx實現(xiàn)左側(cè)欄菜單無限層級封裝 超級詳細GitBook和GitLab集成步驟【linux環(huán)境】 1.1、項目創(chuàng)建 執(zhí)行以下代碼將vite將會自動生成初始的 vite4+vue3+ts的項目模板,pnpm、npm、yarn 選擇一種執(zhí)

    2024年02月04日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包