vue3官方文档
vite使用及创建项目
官方文档
使用npm
使用 Yarn
使用 PNPM
根据提示即可
vue-router的使用
官方文档
安装
使用npm
1
| npm install vue-router@4
|
使用yarn
使用
==1.在src下新建 router/index.ts==
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 { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
const routes: Array<RouteRecordRaw> = [ { path: '/', name: '组件名', redirect: '/' , component: () => import('组件路径'), children:[{path:'',name:'',components:{}}] }, { path: '/', name: '组件名', component: () => import('组件路径'), }, ]
const router = createRouter({ history: createWebHashHistory(), routes })
export default router
|
==2.在main.ts中挂载使用==
1 2 3 4
| import router from './router' const app = createApp(App) app.use(router) app.mount('#app')
|
==3.router-link和router-view==
1 2
| <!-- 使用to='/路径'来指定链接 --> <router-link to="/">Go to sw</router-link>
|
1 2
| <!-- 放到想要呈现路由的地方 --> <router-view></router-view>
|
路由传参
query传参
==在需要传参的组件中==
1 2 3 4 5 6 7 8 9 10 11 12
| import {useRouter } from 'vue-router'; const router = useRouter();
const 事件名 = () => { router.push({ path: '/目标路径', query: { msg: '这是路由传入的参数' } }) };
|
==在需要接收参数的组件中==
1 2 3
| import { useRoute} from 'vue-router'; const route=useRoute() const 接收变量名=route.query.msg
|
或者直接使用
一定要注意 router和route的区别!!
router传参、route接收
小菠萝Pinia
小菠萝文档
相比vuex的优势
- Pinia 提供了一个更简单的 API,具有更少的规范
- vue3
- 可以更好的兼容TypeScript
安装
使用
==1.在main.ts挂载==
1 2 3
| import { createPinia } from 'pinia' const pinia = createPinia() app.use(pinia)
|
==2.在src中创建容器文件夹store及index.ts==
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
| import { defineStore } from 'pinia' export const 函数名 = defineStore('唯一id', { state: () => { return { } }, getters: { }, actions: { }
})
|
==3.在需要使用的组件中==
1 2 3 4 5 6
| import {函数名} from './store.index.ts' const mypinia=函数名()
mypinia.属性名 mypinia.方法名
|
数据更新
在使用的组件中更新 $patch
用 $patch 实现批量更新
1 2 3 4 5 6
| const function=()=>{ mypinia.$patch(state=>{ state.属性1++ state.属性2=····· }) }
|
逻辑过多时可以封装到actions中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| actions:{ 函数名(){ this.属性1++ this.属性2····· } }
actions:{ 函数名(){ this.$patch(state=>{ state.属性1++ state.属性2=····· }) } }
|
使用时调用: mypinia.函数名( )
Element+
官方文档
安装
1
| npm install element-plus --save
|
使用
1 2 3 4 5 6 7 8
| import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import * as ElementPlusIconsVue from '@element-plus/icons-vue'
for (const [key, component] of Object.entries(ElementPlusIconsVue)) { app.component(key, component) } app.use(ElementPlus)
|
axios
官方文档
安装
简易封装
创建http文件夹,并新建index.ts
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
| import axios from 'axios'
const $http = axios.create({ baseURL: '', timeout: 1000, });
$http.interceptors.request.use(function (config) { return config; }, function (error) { return Promise.reject(error); });
$http.interceptors.response.use(function (response) { return response; }, function (error) { return Promise.reject(error); });
export default $http
|
在新建一个文件夹apis,将所有用到的api封装于此
1 2 3 4 5
| import $http from "../http/index";
export const getBanner = () => $http.get('/json/banner.json',{ params: { way:'这样传参' } })
export const GetSaying = (id: number) => $http.get(`https://paul.ren/api/say?id=${id}`)
|
使用class封装
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
| import axios, { AxiosRequestConfig,AxiosResponse } from 'axios' const defaultConfig = { baseUrl: '', timeout: 1500, } class Http { constructor() { this.HttpInterceptorsRequest() this.HttpInterceptorsResponse() } private static axiosInstance = axios.create(defaultConfig) private HttpInterceptorsRequest() { Http.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => { return config }, err => { return Promise.reject(err) }) } private HttpInterceptorsResponse() { Http.axiosInstance.interceptors.response.use((config: AxiosResponse) => { return config }, err => { return Promise.reject(err) }) }
public httpGet<T>(url: string, params: AxiosRequestConfig): Promise<T> { return Http.axiosInstance.get(url, params).then(res => res.data).catch() } public httpPost<T>(url: string, params: AxiosRequestConfig): Promise<T> { return Http.axiosInstance.post(url, params).then(res => res.data).catch() } }
export const http = new Http()
|
使用
1 2 3 4 5 6 7
| import { GetSaying } from "../http/apis"; const saying = ref(); onMounted(async () => { await GetSaying().then((res) => { saying= res.data; }); });
|
使用eslint
1 2
| npm install eslint -D npx eslint --init
|
按需选择配置并安装即可
使用Sass
安装完成后,还需要在vite.config.ts中添加配置
1 2 3 4 5 6 7 8 9 10 11 12
| export default defineConfig({ css: { preprocessorOptions: { scss: { additionalData: `` }, styl: { additionalData: `` } } } })
|
在src/assets下新建scss文件
1 2 3 4
| scss: { additionalData: `@import "src/assets/scss/variable.scss";` },
|
在需要使用的页面设置
1 2 3
| <style scoped lang='scss'> @import '../../assets/scss/home/index.scss'; </style>
|