vue中路由傳參以及跨組件傳參詳解
路由跳轉(zhuǎn)
this.$router.push('/course'); this.$router.push({name: course}); this.$router.go(-1); this.$router.go(1); <router-link to="/course">課程頁(yè)</router-link> <router-link :to="{name: 'course'}">課程頁(yè)</router-link>
路由傳參
方法一
router.js
this.$router.push('/course'); this.$router.push({name: course}); this.$router.go(-1); this.$router.go(1); <router-link to="/course">課程頁(yè)</router-link> <router-link :to="{name: 'course'}">課程頁(yè)</router-link>
跳轉(zhuǎn).vue
<template> <!-- 標(biāo)簽跳轉(zhuǎn) --> <router-link :to="`/course/${course.id}/detail`">{{ course.name }}</router-link> </template> <script> // ... goDetail() { // 邏輯跳轉(zhuǎn) this.$router.push(`/course/${this.course.id}/detail`); } </script>
接收.vue
created() { let id = this.$route.params.id; }
在路由路徑中如果寫(xiě)了如:id這樣的路由匹配(“:”相當(dāng)于后端的匹配,“id”就相當(dāng)于有名分組的名字)。
方法二
router.js
created() { let id = this.$route.params.id; }
跳轉(zhuǎn).vue
<template> <!-- 標(biāo)簽跳轉(zhuǎn) --> <router-link :to="{ name: 'course-detail', query: {id: course.id} }">{{ course.name }}</router-link> </template> <script> // ... goDetail() { // 邏輯跳轉(zhuǎn) this.$router.push({ name: 'course-detail', query: { id: this.course.id } }); } </script>
接收.vue
<template> <!-- 標(biāo)簽跳轉(zhuǎn) --> <router-link :to="{ name: 'course-detail', query: {id: course.id} }">{{ course.name }}</router-link> </template> <script> // ... goDetail() { // 邏輯跳轉(zhuǎn) this.$router.push({ name: 'course-detail', query: { id: this.course.id } }); } </script>
可以完成跨組件傳參的四種方式
// 1) localStorage:永久存儲(chǔ)數(shù)據(jù)
// 2) sessionStorage:臨時(shí)存儲(chǔ)數(shù)據(jù)(刷新頁(yè)面數(shù)據(jù)不重置,關(guān)閉再重新開(kāi)啟標(biāo)簽頁(yè)數(shù)據(jù)重置)
// 3) cookie:臨時(shí)或永久存儲(chǔ)數(shù)據(jù)(由過(guò)期時(shí)間決定)
// 4) vuex的倉(cāng)庫(kù)(store.js):臨時(shí)存儲(chǔ)數(shù)據(jù)(刷新頁(yè)面數(shù)據(jù)重置)
vuex倉(cāng)庫(kù)插件
store.js配置文件
export default new Vuex.Store({ state: { title: '默認(rèn)值' }, mutations: { // mutations 為 state 中的屬性提供setter方法 // setter方法名隨意,但是參數(shù)列表固定兩個(gè):state, newValue setTitle(state, newValue) { state.title = newValue; } }, actions: {} })
在任意組件中給倉(cāng)庫(kù)變量賦值
this.$store.state.title = 'newTitle' // 第一種 this.$store.commit('setTitle', 'newTitle') //第二種
第二種要mutations中提供的setter方法,雖然這個(gè)方法最終也是將值傳到state中,但是我們可以這個(gè)setter方法里寫(xiě)一些驗(yàn)證之類的判斷
在任意組件中取倉(cāng)庫(kù)變量的值
console.log(this.$store.state.title)
vue-cookie插件
安裝
console.log(this.$store.state.title)
main.js 配置
// 第一種 import cookies from 'vue-cookies' // 導(dǎo)入插件 Vue.use(cookies); // 加載插件 new Vue({ // ... cookies, // 配置使用插件原型 $cookies }).$mount('#app'); // 第二種 import cookies from 'vue-cookies' // 導(dǎo)入插件 Vue.prototype.$cookies = cookies; // 直接配置插件原型 $cookies
使用
// 增(改): key,value,exp(過(guò)期時(shí)間) // 1 = '1s' | '1m' | '1h' | '1d' this.$cookies.set('token', token, '1y'); // 查:key this.token = this.$cookies.get('token'); // 刪:key this.$cookies.remove('token');
注:cookie一般都是用來(lái)存儲(chǔ)token的
// 1) 什么是token:安全認(rèn)證的字符串
// 2) 誰(shuí)產(chǎn)生的:后臺(tái)產(chǎn)生
// 3) 誰(shuí)來(lái)存儲(chǔ):后臺(tái)存儲(chǔ)(session表、文件、內(nèi)存緩存),前臺(tái)存儲(chǔ)(cookie)
// 4) 如何使用:服務(wù)器先生成反饋給前臺(tái)(登陸認(rèn)證過(guò)程),前臺(tái)提交給后臺(tái)完成認(rèn)證(需要登錄后的請(qǐng)求)
// 5) 前后臺(tái)分離項(xiàng)目:后臺(tái)生成token,返回給前臺(tái) => 前臺(tái)自己存儲(chǔ),發(fā)送攜帶token請(qǐng)求 => 后臺(tái)完成token校驗(yàn) => 后臺(tái)得到登陸用戶
(上面提到過(guò)用cookie也可以完成跨組件傳參,由于cookies中沒(méi)有設(shè)置默認(rèn)值,所以如果是空值的話,在視圖函數(shù)中拿到的也是空,所以我們要在視圖函數(shù)中也設(shè)置一個(gè)默認(rèn)值,然后對(duì)用cookies傳過(guò)來(lái)的值進(jìn)行判斷如果不是空值的時(shí)候,用它替換默認(rèn)值,然后渲染上去)
axios插件
安裝
>: cnpm install axios
main.js配置
import axios from 'axios' // 導(dǎo)入插件 Vue.prototype.$axios = axios; // 直接配置插件原型 $axios
使用
this.axios({ url: '請(qǐng)求接口', method: 'get|post請(qǐng)求', data: {post等提交的數(shù)據(jù)}, params: {get提交的數(shù)據(jù)} }).then(請(qǐng)求成功的回調(diào)函數(shù)).catch(請(qǐng)求失敗的回調(diào)函數(shù))
案例
// get請(qǐng)求 this.$axios({ url: 'http://127.0.0.1:8000/test/ajax/', method: 'get', params: { username: this.username } }).then(function (response) { console.log(response) }).catch(function (error) { console.log(error) }); // post請(qǐng)求 this.$axios({ url: 'http://127.0.0.1:8000/test/ajax/', method: 'post', data: { username: this.username } }).then(function (response) { console.log(response) }).catch(function (error) { console.log(error) });
跨域問(wèn)題(同源策略)
// 后臺(tái)接收到前臺(tái)的請(qǐng)求,可以接收前臺(tái)數(shù)據(jù)與請(qǐng)求信息,發(fā)現(xiàn)請(qǐng)求的信息不是自身服務(wù)器發(fā)來(lái)的請(qǐng)求,拒絕響應(yīng)數(shù)據(jù),這種情況稱之為 - 跨域問(wèn)題(同源策略 CORS) // 導(dǎo)致跨域情況有三種 // 1) 端口不一致 // 2) IP不一致 // 3) 協(xié)議不一致 // Django如何解決 - django-cors-headers模塊 // 1) 安裝:pip3 install django-cors-headers // 2) 注冊(cè): INSTALLED_APPS = [ ... 'corsheaders' ] // 3) 設(shè)置中間件: MIDDLEWARE = [ ... 'corsheaders.middleware.CorsMiddleware' ] // 4) 設(shè)置跨域: CORS_ORIGIN_ALLOW_ALL = True
element-ui插件
安裝
>: cnpm i element-ui -S
main.js配置
import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI);
使用
依照官網(wǎng) https://element.eleme.cn/#/zh-CN/component/installation api
總結(jié)
到此這篇關(guān)于vue中路由傳參以及跨組件傳參的文章就介紹到這了,更多相關(guān)vue路由及跨組件傳參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于el-table封裝的可拖拽行列、選擇列組件的實(shí)現(xiàn)
本文主要介紹了基于el-table封裝的可拖拽行列、選擇列組件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12解決Element中el-date-picker組件不回填的情況
這篇文章主要介紹了解決Element中el-date-picker組件不回填的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11vue3+vite中開(kāi)發(fā)環(huán)境與生產(chǎn)環(huán)境全局變量配置指南
最近在使用vite生成項(xiàng)目,這篇文章主要給大家介紹了關(guān)于vue3+vite中開(kāi)發(fā)環(huán)境與生產(chǎn)環(huán)境全局變量配置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08vue中的路由傳值與重調(diào)本路由改變參數(shù)
這篇文章主要介紹了vue中的路由傳值與重調(diào)本路由改變參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Composition Api封裝業(yè)務(wù)hook思路示例分享
這篇文章主要為大家介紹了Composition Api封裝業(yè)務(wù)hook的思路示例分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07淺談vue3中effect與computed的親密關(guān)系
這篇文章主要介紹了淺談vue3中effect與computed的親密關(guān)系,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10