vue3中使用vuex和vue-router的詳細(xì)步驟
vuex
首先,講解一下vuex,v2和v3在vuex的使用上一樣,差別主要是在版本上,vue2中的vuex的版本必須是4版本以下,而v3的vuex的版本必須是4版本及以上
安裝
cnpm i vuex@4 --save
使用步驟
src中先創(chuàng)建一個(gè)store文件夾,文件夾中創(chuàng)建一個(gè)index.js文件
index.js導(dǎo)入vuex中需要使用的依賴(lài)包c(diǎn)reateStore()
createStore創(chuàng)建一個(gè)vuex的對(duì)象拋出即可
main.js中直接導(dǎo)入這個(gè)對(duì)象即可
index.js文件
//1. 導(dǎo)入依賴(lài)
//導(dǎo)入vuex的函數(shù)內(nèi)容
import {createStore} from "vuex";
//調(diào)用函數(shù)進(jìn)行配置
const store = createStore({
state:{
num:100,
},
mutations:{
plus(state){
state.num++;
}
},
getters:{},
actions:{},
modules:{},//分模塊
// plugins:[],
})
//拋出對(duì)象的內(nèi)容信息
export default store;main.js文件
//導(dǎo)入vuex對(duì)象 const app = createApp(App); //引入vuex的操作信息 import store from "./store/index"; app.use(store);
組件中使用vuex
<template>
<div>
<button @click="plus">num++</button>
{{$store.state.num}}
</div>
</template>
<script>
import {useStore} from 'vuex'
export default {
setup() {
const store=useStore()
const plus=()=>{
store.commit('plus')
}
return {plus}
},
}
</script>vue-router
安裝
cnpm i vue-router@4 --save
基本配置
src中創(chuàng)建一個(gè)文件夾router,router中新建一個(gè)index.js
index.js中導(dǎo)入創(chuàng)建路由對(duì)象的api createRouter,拋出對(duì)象
main.js中導(dǎo)入路由對(duì)象,掛載在app中去
index.js
import {createRouter, createWebHistory} from 'vue-router'
const routes = [
{
name: 'home',
path: '/',
component: () => import('../views/Home.vue')
},
{
name: 'login',
path: '/log',
component: () => import('../views/Login.vue')
}
];
const router = createRouter({
history: createWebHistory(),
routes
})
export default routermain.js
import router from "./router/index"; app.use(router);
vue3中路由的使用
<template>
<div>
<h3>主頁(yè)面Home</h3>
<button @click="goMy">My</button>
</div>
</template>
<script>
import {useRouter} from "vue-router"
export default {
setup(){
const router = useRouter();
const goMy = ()=>{
router.push("/my?id=100");
}
return {
goMy
}
}
}
</script>useRoute
<template>
<div>
<h3>個(gè)人中心</h3>
</div>
</template>
<script>
import {useRoute} from "vue-router"
export default {
setup(){
const route = useRoute();
console.log(route);
let {query} = route;
console.log(query);
}
}
</script>axios的安裝配置
安裝
cnpm i axios --save
main.js
import axios from "axios";
const app = createApp(App);
app.config.globalProperties.$axios = axios;
app.use(store).use(router).mount('#app')在src中創(chuàng)建一個(gè)pubilc文件夾,然后創(chuàng)建一個(gè)index.js文件,文件中配置
//導(dǎo)入axios
import axios from "axios";
//創(chuàng)建對(duì)象相關(guān)的信息
const Server = axios.create({
baseURL:"",
timeout:5000,
})
//配置前置攔截器或者后置攔截器
Server.interceptors.request.use((confirm)=>{
return config;
},error=>Promise.reject(error));
//相應(yīng)攔截器,數(shù)據(jù)返回,到達(dá)客戶端之前觸發(fā)。
Server.interceptors.response.use((response)=>{
return response;
},error=>Promise.reject(error))
export default Server;vue3自定義指令
非setup語(yǔ)法糖中,自定義指令的作用跟vue2中的作用差不多,然后語(yǔ)法糖中的話,可以定義一個(gè)v開(kāi)頭的函數(shù)對(duì)象,當(dāng)成自定義指令的作用。
分為全局自定義指令局部自定自定義
bind inserted() update() componentUpdated() unbind()
自定義指令生命周期函數(shù)變化
- created - 新增!在元素的 attribute 或事件監(jiān)聽(tīng)器被應(yīng)用之前調(diào)用。
- bind → beforeMount
- inserted → mounted
- beforeUpdate:新增!在元素本身被更新之前調(diào)用,與組件的生命周期鉤子十分相似。
- update → 移除!該鉤子與 updated 有太多相似之處,因此它是多余的。請(qǐng)改用 updated。
- componentUpdated → updated
- beforeUnmount:新增!與組件的生命周期鉤子類(lèi)似,它將在元素被卸載之前調(diào)用。
- unbind -> unmounted
- el ,當(dāng)前元素, binding 傳遞的數(shù)據(jù)
全局自定義指令
main.js
app.directive('focus',{
mounted(el,binding){
console.log(el,binding);
el.focus()
el.value=binding.value
}
})組件中使用全局自定義指令
<template>
<div>
<input type="text" v-focus:change.native="123123" placeholder="輸入內(nèi)容"/>
</div>
</template>局部自定義指令
在局部自定義指令中給他一個(gè)拖拽事件
<template>
<div>
<div class="box" v-drag></div>
</div>
</template>
<script>
import {useStore} from 'vuex'
import {useRouter} from 'vue-router'
export default {
directives: {
drag: {
mounted(el, binding) {
//鼠標(biāo)按下
el.onmousedown = function () {
var event = event || window.event;
//獲取坐標(biāo)值
var x1 = event.clientX;
var y1 = event.clientY;
//還需要獲取偏移值
var L = el.offsetLeft;
var T = el.offsetTop;
console.log(x1, y1, L, T);
//鼠標(biāo)移動(dòng)
document.onmousemove = function (event) {
//獲取坐標(biāo)值
var x2 = event.clientX;
var y2 = event.clientY;
var l = x2 - x1 + L;
var maxW = document.documentElement.clientWidth - el.clientWidth;
if(l<=0){
l = 0;
}
if(l>=maxW){
l = maxW;
}
//改變盒子的偏移值
el.style.left = l+ "px";
el.style.top = y2 - y1 + T + "px";
};
//異動(dòng)結(jié)束后,鼠標(biāo)松開(kāi)的時(shí)候觸發(fā)的操作
document.onmouseup = function(){
document.onmousemove = null;
}
};
},
},
},
setup() {}
}
</script>
<style scoped>
.box{
width: 100px;
height: 100px;
background-color: lightcoral;
}
</style>vue3中的插槽使用
slot理解
我們經(jīng)常會(huì)有封裝組件的需求,組件需要的往往不只有數(shù)據(jù),一般我們通過(guò)組件通信傳遞的都是我們的基本數(shù)據(jù)類(lèi)型或者是引用數(shù)據(jù)類(lèi)型,如果我們想要傳遞一些標(biāo)簽屬性,那么我們就要使用插槽來(lái)進(jìn)行實(shí)現(xiàn)
具名插槽就是給slot標(biāo)簽添加name=""屬性,使用是在template標(biāo)簽中用#name綁定使用
作用域插槽就是使用插槽傳遞數(shù)據(jù),傳遞的數(shù)據(jù)直接綁在slot身上。使用是在template標(biāo)簽上#name="scope",傳遞的值就在scope里面
#號(hào)是v-slot指令的縮寫(xiě)
簡(jiǎn)單插槽
到此這篇關(guān)于vue3中使用vuex和vue-router的文章就介紹到這了,更多相關(guān)vue3使用vuex和vue-router內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue數(shù)組中不滿足條件跳出循環(huán)問(wèn)題
這篇文章主要介紹了vue數(shù)組中不滿足條件跳出循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
詳解vue 動(dòng)態(tài)加載并注冊(cè)組件且通過(guò) render動(dòng)態(tài)創(chuàng)建該組件
這篇文章主要介紹了vue 動(dòng)態(tài)加載并注冊(cè)組件且通過(guò) render動(dòng)態(tài)創(chuàng)建該組件,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Vue3?vite配置css?的sourceMap及文件引用配置別名的過(guò)程
這篇文章主要介紹了Vue3 vite配置css的sourceMap,及文件引用配置別名的過(guò)程,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Vuex實(shí)現(xiàn)購(gòu)物車(chē)小功能
這篇文章主要為大家詳細(xì)介紹了Vuex實(shí)現(xiàn)購(gòu)物車(chē)小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
vue-cropper插件實(shí)現(xiàn)圖片截取上傳組件封裝
這篇文章主要為大家詳細(xì)介紹了vue-cropper插件實(shí)現(xiàn)圖片截取上傳組件封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
vue watch偵聽(tīng)器有無(wú)immediate的運(yùn)行順序問(wèn)題
這篇文章主要介紹了vue watch偵聽(tīng)器有無(wú)immediate的運(yùn)行順序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue.js如何優(yōu)雅的進(jìn)行form validation
Vue.js如何優(yōu)雅的進(jìn)行form validation,針對(duì)此問(wèn)題,給出了多個(gè)網(wǎng)友的回答,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
ElementUI 詳細(xì)分析DatePicker 日期選擇器實(shí)戰(zhàn)
這篇文章主要介紹了ElementUI詳細(xì)分析DatePicker 日期選擇器實(shí)戰(zhàn)教程,本文通過(guò)實(shí)例代碼圖文介紹給大家講解的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
Vue利用Blob下載原生二進(jìn)制數(shù)組文件
這篇文章主要為大家詳細(xì)介紹了Vue利用Blob下載原生二進(jìn)制數(shù)組文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09

