Vue3-(三)Vue3+TypeScript+Vite2创建项目
本文最后更新于:April 16, 2022 am
Vue(读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式JavaScript框架。与其它框架不同的是,Vue被设计为可以自底向上逐层应用。Vue的核心库只关注视图层,方便与第三方库或既有项目整合。另一方面,Vue 完全有能力驱动采用单文件组件和Vue生态系统支持的库开发的复杂单页应用。Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。
目录
Vite创建项目
安装依赖
| npm i vue-router@4 npm i axios -S npm install @element-plus/icons-vue npm install element-plus --save npm i vuex@next npm install @types/node -D
|
配置路由(router)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'
import path from 'path'
export default defineConfig({ plugins: [vue()], resolve: { extensions: ['.js', '.ts', '.vue', '.json'], alias: { '@': path.resolve(__dirname, 'src') } },
})
|
| import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
const routes : Array<RouteRecordRaw> = [ { path: '/', name: 'Home', component: ()=>import('../views/home/index.vue') } ] const router = createRouter({ history: createWebHistory(), routes })
export default router
|
| import { createApp } from 'vue' import App from './App.vue' import router from './router/index'
const app = createApp(App) app.use(router) app.mount('#app')
|
配置Element-Plus
| import { createApp } from 'vue' import App from './App.vue' import router from './router/index' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import * as ElIcons from '@element-plus/icons-vue'
const app = createApp(App) for (const icname in ElIcons){ app.component(icname,ElIcons[icname]) } app.use(router) app.use(ElementPlus) app.mount('#app')
|
配置Axios
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import axios from 'axios'
const request = axios.create({ baseURL: 'http://localhost:8081', timeout: 10000 })
request.interceptors.request.use(config => { config.headers['Content-Type'] = 'application/json;charset=utf-8';
return config }, error => { return Promise.reject(error) });
request.interceptors.response.use( response => { let res = response.data; if (response.config.responseType === 'blob') { return res } if (typeof res === 'string') { res = res ? JSON.parse(res) : res } return res; }, error => { return Promise.reject(error) } )
export default request
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import { createApp } from 'vue' import App from './App.vue' import router from './router/index' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import * as ElIcons from '@element-plus/icons-vue' import request from "./request/request";
const app = createApp(App) for (const icname in ElIcons){ app.component(icname,ElIcons[icname]) } app.config.globalProperties.$http = request app.use(router) app.use(ElementPlus) app.mount('#app')
|
配置vuex
| import { createStore } from 'vuex'
export default createStore({ state: { }, mutations: { }, actions: { }, modules: { } })
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import { createApp } from 'vue' import App from './App.vue' import router from './router/index' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import * as ElIcons from '@element-plus/icons-vue' import store from './store/index' import request from "./request/request";
const app = createApp(App) for (const icname in ElIcons){ app.component(icname,ElIcons[icname]) } app.config.globalProperties.$http = request app.use(router) app.use(store) app.use(ElementPlus) app.mount('#app')
|
安装scss/sass
| npm install node-sass sass-loader sass -D
|
完整配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import { createApp } from 'vue' import App from './App.vue' import router from './router/index' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import * as ElIcons from '@element-plus/icons-vue' import store from './store/index' import request from "./request/request";
const app = createApp(App) for (const icname in ElIcons){ app.component(icname,ElIcons[icname]) } app.config.globalProperties.$http = request app.use(router) app.use(store) app.use(ElementPlus) app.mount('#app')
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'
import path from 'path'
export default defineConfig({ plugins: [vue()], resolve: { extensions: ['.js', '.ts', '.vue', '.json'], alias: { '@': path.resolve(__dirname, 'src') } },
})
|