vue3中router路由以及vuex的store使用解析
vue3 router路由及vuex store使用
1.路由
<script> import { useRouter, useRoute } from 'vue-router' export default { ? setup() { ? ? const router = useRouter() // 組件內(nèi)路由 ? ? const route = useRoute() // 組件內(nèi)路由信息 ? } } </script>
2.vuex
創(chuàng)建store
import { createStore } from 'vuex' import login from './login' const store = createStore({ ? state: {}, ? mutations: {}, ? actions: {}, ? modules: { ? ? login ? } }) export default store
組件內(nèi)使用store
<script> import { useStore } from 'vuex' export default { ? setup() { ? ? const store = useStore() ? ? const state = store.state ? ??? ?const methods = { ? ??? ? ?// 處理commit ? ? ? handleMutation: () => { ? ? ? ? store.commit(...) ? ? ? }, ? ? ? // 處理dispatch ? ? ? handleAction: () => { ? ? ? ? store.dispatch(...) ? ? ? } ? ??? ?} ? ? return { ? ? ? ...methods? ? ? } ? } } </script>
vue3中router路由和vuex的store使用,獲取對(duì)象基本使用
vue3中router和store使用方法
因?yàn)樵趘ue3的setup內(nèi)不能使用this對(duì)象,所有所有直接通過(guò)this來(lái)訪問(wèn)的方法都不能使用了。
那么在vue3中怎么訪問(wèn)this.r o u t e r 或 者 t h i s . router 或者 this.router或者this.route呢?
記住一條規(guī)則:vue3中基本上所有通過(guò)this來(lái)訪問(wèn)的對(duì)象都換成useXxx的方式來(lái)獲取。
比如說(shuō)router,可以通過(guò)useRouter和 useRoute來(lái)獲取router,route對(duì)象
1、企業(yè)開(kāi)發(fā)Router全局配置
企業(yè)開(kāi)發(fā)中在src/router/index.js中全局配置,并在main.js中添加到Vue對(duì)象
import { createRouter, createWebHistory } from "vue-router"; // 路由規(guī)則 const routes = [ ? { ? ? path: "/", ? ? name: "主頁(yè)", ? ? meta: ["istabbar"], ? ? component: () => import("../views/Home.vue"), ? }, ]; //根據(jù)路由規(guī)則創(chuàng)建路由 const router = createRouter({ ? history: createWebHistory(""), ? routes, }); export default router;
在main.js中添加到Vue對(duì)象
import { createApp } from "vue"; import App from "./App.vue"; import router from "./router"; // 添加路由router引入 // 創(chuàng)建VUE對(duì)象 createApp(App) ? .use(router) // 使用.use(router)添加路由router ? .mount("#app");
配置完后就可以全局使用了
1.1、Router獲取及使用
先引入vue-router,再獲取對(duì)象:
import { useRouter } from “vue-router”; const router = useRouter();
但是這也不是唯一的方法,傳統(tǒng)的和VUE3的使用方法如下:
這里提供了三種方法用于獲取 router 對(duì)象
<template> ? <div class="page"> ? ? 因?yàn)樵趕etup內(nèi)部沒(méi)有this訪問(wèn)權(quán)限,不能直接使用this訪問(wèn)this.$router 或者 ? ? this.$route。所有使用useRouter, useRoute來(lái)獲取 ? </div> </template>
<script> import { getCurrentInstance } from "vue"; import { useRouter } from "vue-router"; export default { ? setup() { ? ? // 第一種方法:獲取路由對(duì)象 router 的方法1 ? ? const vue = getCurrentInstance(); ? ? const router1 = vue.ctx.$router; ? ? // 第二種方法:獲取路由對(duì)象 router 的方法2 ? ? const router2 = useRouter(); ? ? ?? ? ? // 下面提供了router對(duì)應(yīng)的方法使用大全 ? ? // ------------------------------------------------------ ? ? // router1.addRoute(parentOrRoute, route) ? ? // router1.afterEach(回調(diào)函數(shù)) ? ? // router1.back() 等價(jià)于 go(-1) ? ? // router1.beforeEach(回調(diào)函數(shù)) ? ? // router1.beforeResolve(回調(diào)函數(shù)) ? ? // router1.currentRoute 獲取當(dāng)前路由:如: {_rawValue: {…}, _shallow: true, __v_isRef: true, _value: {…}} ? ? // router1.forward() 等價(jià)于 go(1) ? ? // router1.getRoutes: ? getRoutes() ? ? // router1.go(delta) 等價(jià)于 routerHistory.go(delta) 跳到指定歷史記錄 ? ? // router1.hasRoute(name) 判斷是否有對(duì)應(yīng)的路由 ? ? // router1.isReady() 判斷路由是否準(zhǔn)備跳轉(zhuǎn) ? ? // router1.onError(回調(diào)函數(shù)) 當(dāng)發(fā)生錯(cuò)誤的時(shí)候執(zhí)行的 ? ? // router1.options 獲取所有路由信息 {history: {…}, routes: Array(5)} ? ? // router1.push(to) 跳轉(zhuǎn)到指定路由對(duì)應(yīng)的頁(yè)面,有歷史記錄 ? ? // router1.removeRoute(name) 動(dòng)態(tài)的刪除某個(gè)路由 ? ? // router1.replace(to) 直接跳轉(zhuǎn)到指定路由頁(yè)面,沒(méi)有歷史記錄 ? ? // router1.resolve(rawLocation, currentLocation) ?可以自定義跳轉(zhuǎn)參數(shù)的方法 ? ? return {}; ? }, ? mounted() { ? ? // 第三種方法:獲取路由對(duì)象 router 的方法3 ? ? console.log(this.$router); ? }, }; </script>
1.2、Route獲取及使用
先引入vue-router,再獲取對(duì)象:
import { useRouter } from “vue-router”; const router = useRouter();
但是這也不是唯一的方法,傳統(tǒng)的和VUE3的使用方法如下:
這里提供了三種方法用于獲取 router 對(duì)象
<template> ? <div class="page"> ? ? 因?yàn)樵趕etup內(nèi)部沒(méi)有this訪問(wèn)權(quán)限,不能直接使用this訪問(wèn)this.$router 或者 ? ? this.$route。所有使用useRouter, useRoute來(lái)獲取 ? </div> </template>
<script> import { getCurrentInstance } from "vue"; import { useRoute } from "vue-router"; export default { ? setup() { ? ? // 第一種方法:獲取路由對(duì)象 router 的方法1 ? ? const vue = getCurrentInstance(); ? ? const route1 = vue.ctx.$router.currentRoute.value; ? ? console.log(route1); ? ? // 第二種方法:獲取路由對(duì)象 router 的方法2 ? ? const route2 = useRoute(); ? ? console.log(route2); ? ? // 下面提供了route對(duì)應(yīng)的屬性使用大全 ? ? // ------------------------------------------------------ ? ? // fullPath: "/user" ?完整路由路徑 ? ? // hash: "" 錨點(diǎn)部分 ? ? // href: "/user" ?跳轉(zhuǎn)來(lái)的時(shí)候的路徑 ? ? // matched: [{…}] ? 路由匹配日規(guī)則數(shù)組 ? ? // meta: {0: "istabbar"} ?路由附加的元數(shù)據(jù) ? ? // name: "個(gè)人中心" 路由的名稱(chēng) ? ? // params: {} ? 路由跳轉(zhuǎn)時(shí)帶過(guò)來(lái)的附加參數(shù),如果是對(duì)象需要轉(zhuǎn)換成JSON格式 ? ? // path: "/user" ?編碼 URL 的 pathname 部分,與路由地址有關(guān) ? ? // query: {} ? 地址附帶的參數(shù) ? ? // redirectedFrom: undefined ?重定向跳轉(zhuǎn)過(guò)來(lái)之前的地址,如果沒(méi)有重定向,則為 undefined。 ? ? return {}; ? }, ? mounted() { ? ? // 第三種方法:獲取路由對(duì)象 router 的方法3 ? ? console.log(this.$route); ? }, }; </script>
2、企業(yè)開(kāi)發(fā)Store全局配置
企業(yè)開(kāi)發(fā)中在src/store/index.js中全局配置,并在main.js中添加到Vue對(duì)象
import { createStore } from "vuex"; //創(chuàng)建存儲(chǔ)對(duì)象 export default createStore({ ? // 需要存儲(chǔ)的值都放在這里面 ? state() { ? ? return { ? ? ? count: 0,?? ?// 在視圖中通過(guò)$store.state.count來(lái)獲取 ? ? }; ? }, ? // 在其他視圖中通過(guò) $store.commit('setState', 10) 使用,用于修改stor存的值 ? mutations: { ? ? setState(state, count) {?? ?// 只能接受兩個(gè)參數(shù),用于修改store存的值 ? ? ? state.count = count; ? ? }, ? }, ? // 相當(dāng)于組件的計(jì)算屬性 通過(guò) $store.getters.doubleCount 獲取計(jì)算后的值 ? getters: { ? ? doubleCount(state) { ? ? ? return state.count * 2; ? ? }, ? }, ? // 異步任務(wù) 不會(huì)改變state 通過(guò) $store.dispath('doubleCount') 使用 ? actions: { ? ? doubleCount(context) { ? ? ? context.commit("doubleCount"); ? ? }, ? }, ? // store的下級(jí)store 方便大型項(xiàng)目復(fù)雜數(shù)據(jù)管理,這里面相當(dāng)于可以在放置一個(gè)和外面這些一樣的模塊 ? modules: {}, });
在main.js中添加到Vue對(duì)象
import { createApp } from "vue"; import App from "./App.vue"; import router from "./router"; // 添加路由router引入 import store from "./store"; // 添加全局存儲(chǔ)vuex引入 // 創(chuàng)建VUE對(duì)象 createApp(App) ? .use(router) // 使用.use(router)添加路由router ? .use(store) // 使用.use(store)添加全局存儲(chǔ)vuex ? .mount("#app");
配置完后就可以全局使用了
2.1、Store獲取及使用
先引入vuex,再獲取對(duì)象:
import { useStore } from “vuex”; const store = useStore();
這里提供了三種方法用于獲取 router 對(duì)象
<template> ? <div class="page"> ? ? 因?yàn)樵趕etup內(nèi)部沒(méi)有this訪問(wèn)權(quán)限,不能直接使用this訪問(wèn)this.$router 或者 ? ? this.$route。所有使用useRouter, useRoute來(lái)獲取 ? </div> </template>
<script> import { getCurrentInstance } from "vue"; import { useStore } from "vuex"; export default { ? setup() { ? ? // 第一種方法:獲取路由對(duì)象 router 的方法1 ? ? const vue = getCurrentInstance(); ? ? const store1 = vue.ctx.$store; ? ? console.log(store1); ? ? // 第二種方法:獲取路由對(duì)象 router 的方法2 ? ? const store2 = useStore(); ? ? console.log(store2); ? ? // 下面提供了:store 對(duì)應(yīng)的屬性使用大全 ? ? // ------------------------------------------------------ ? ? // commit(type, payload, options2) 在其他視圖中使用mutations中定義的方法 ? ? // dispatch(type, payload) 異步任務(wù) 不會(huì)改變state 在其他視圖中使用actions中定義的方法 ? ? // getters: {} ?相當(dāng)于組件的計(jì)算屬性 通過(guò) $store.getters.doubleCount 獲取計(jì)算后的值 ? ? // state: (...) ?store中存儲(chǔ)的值,就是通過(guò)這個(gè)在視圖中獲取store存儲(chǔ)的值 ? ? return {}; ? }, ? mounted() { ? ? // 第三種方法:獲取路由對(duì)象 store的方法3 ? ? console.log(this.$store); ? }, }; </script>
功能快捷鍵
- 撤銷(xiāo):Ctrl/Command + Z
- 重做:Ctrl/Command + Y
- 加粗:Ctrl/Command + B
- 斜體:Ctrl/Command + I
- 標(biāo)題:Ctrl/Command + Shift + H
- 無(wú)序列表:Ctrl/Command + Shift + U
- 有序列表:Ctrl/Command + Shift + O
- 檢查列表:Ctrl/Command + Shift + C
- 插入代碼:Ctrl/Command + Shift + K
- 插入鏈接:Ctrl/Command + Shift + L
- 插入圖片:Ctrl/Command + Shift + G
- 查找:Ctrl/Command + F
- 替換:Ctrl/Command + G
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue3實(shí)現(xiàn)監(jiān)聽(tīng)store中state狀態(tài)變化的簡(jiǎn)單方法
- vuex之this.$store.dispatch()與this.$store.commit()的區(qū)別及說(shuō)明
- Vuex的store中的Module用法及說(shuō)明
- vuex中this.$store.commit和this.$store.dispatch的基本用法實(shí)例
- vue data中如何獲取使用store中的變量
- 在Vue中使用this.$store或者是$route一直報(bào)錯(cuò)的解決
- vue.js的狀態(tài)管理vuex中store的使用詳解
- Vue 關(guān)于Store的用法小結(jié)
相關(guān)文章
vue在項(xiàng)目中實(shí)現(xiàn)base64加密解密的示例代碼
這篇文章主要為大家詳細(xì)介紹了vue在項(xiàng)目中實(shí)現(xiàn)base64加密解密的兩種方法,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,有需要的小伙伴可以了解一下2023-10-10詳解element-ui設(shè)置下拉選擇切換必填和非必填
這篇文章主要介紹了詳解element-ui設(shè)置下拉選擇切換必填和非必填,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06vue中vxe-table虛擬滾動(dòng)列表的使用詳解
vxe-table 是一個(gè)功能強(qiáng)大的 Vue 表格組件,它支持虛擬滾動(dòng)列表作為其核心功能之一,本文主要介紹一下vxe-table的虛擬滾動(dòng)列表功能的使用場(chǎng)景和優(yōu)勢(shì),感興趣的可以了解下2023-12-12vue3.0引入百度地圖并標(biāo)記點(diǎn)的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue3.0引入百度地圖并標(biāo)記點(diǎn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08vue如何實(shí)現(xiàn)點(diǎn)擊選中取消切換
這篇文章主要介紹了vue實(shí)現(xiàn)點(diǎn)擊選中取消切換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05idea編譯器vue縮進(jìn)報(bào)錯(cuò)問(wèn)題場(chǎng)景分析
idea編譯器出現(xiàn)Vue縮進(jìn)報(bào)錯(cuò),怎么解決呢,很多朋友遇到這個(gè)問(wèn)題都很棘手,不知該如何解決,今天小編給大家通過(guò)場(chǎng)景分析介紹解決方案,需要的朋友參考下吧2021-07-07Vue解決echart在element的tab切換時(shí)顯示不正確問(wèn)題
這篇文章主要介紹了Vue解決echart在element的tab切換時(shí)顯示不正確問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue?element-plus中el-input修改邊框border的方法
element樣式還是蠻好的,只是有時(shí)候我們需要做一些調(diào)整,比如el-input的邊框,下面這篇文章主要給大家介紹了關(guān)于vue?element-plus中el-input修改邊框border的相關(guān)資料,需要的朋友可以參考下2022-09-09