路由跳轉
1
2
3
4
5
6
|
this .$router.push( '/course' ); this .$router.push({name: course}); this .$router.go(-1); this .$router.go(1); <router-link to= "/course" >課程頁</router-link> <router-link :to= "{name: 'course'}" >課程頁</router-link> |
路由傳參
方法一
router.js
1
2
3
4
5
6
|
this .$router.push( '/course' ); this .$router.push({name: course}); this .$router.go(-1); this .$router.go(1); <router-link to= "/course" >課程頁</router-link> <router-link :to= "{name: 'course'}" >課程頁</router-link> |
跳轉.vue
1
2
3
4
5
6
7
8
9
10
11
|
<template> <!-- 標簽跳轉 --> <router-link :to= "`/course/${course.id}/detail`" >{{ course.name }}</router-link> </template> <script> // ... goDetail() { // 邏輯跳轉 this .$router.push(`/course/${ this .course.id}/detail`); } </script> |
接收.vue
1
2
3
|
created() { let id = this .$route.params.id; } |
在路由路徑中如果寫了如:id這樣的路由匹配(“:”相當于后端的匹配,“id”就相當于有名分組的名字)。
方法二
router.js
1
2
3
|
created() { let id = this .$route.params.id; } |
跳轉.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<template> <!-- 標簽跳轉 --> <router-link :to= "{ name: 'course-detail', query: {id: course.id} }" >{{ course.name }}</router-link> </template> <script> // ... goDetail() { // 邏輯跳轉 this .$router.push({ name: 'course-detail' , query: { id: this .course.id } }); } </script> |
接收.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<template> <!-- 標簽跳轉 --> <router-link :to= "{ name: 'course-detail', query: {id: course.id} }" >{{ course.name }}</router-link> </template> <script> // ... goDetail() { // 邏輯跳轉 this .$router.push({ name: 'course-detail' , query: { id: this .course.id } }); } </script> |
可以完成跨組件傳參的四種方式
// 1) localStorage:永久存儲數據
// 2) sessionStorage:臨時存儲數據(刷新頁面數據不重置,關閉再重新開啟標簽頁數據重置)
// 3) cookie:臨時或永久存儲數據(由過期時間決定)
// 4) vuex的倉庫(store.js):臨時存儲數據(刷新頁面數據重置)
vuex倉庫插件
store.js配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
export default new Vuex.Store({ state: { title: '默認值' }, mutations: { // mutations 為 state 中的屬性提供setter方法 // setter方法名隨意,但是參數列表固定兩個:state, newValue setTitle(state, newValue) { state.title = newValue; } }, actions: {} }) |
在任意組件中給倉庫變量賦值
1
2
|
this .$store.state.title = 'newTitle' // 第一種 this .$store.commit( 'setTitle' , 'newTitle' ) //第二種 |
第二種要mutations中提供的setter方法,雖然這個方法最終也是將值傳到state中,但是我們可以這個setter方法里寫一些驗證之類的判斷
在任意組件中取倉庫變量的值
1
|
console.log( this .$store.state.title) |
vue-cookie插件
安裝
1
|
console.log(this.$store.state.title) |
main.js 配置
1
2
3
4
5
6
7
8
9
10
11
|
// 第一種 import cookies from 'vue-cookies' // 導入插件 Vue.use(cookies); // 加載插件 new Vue({ // ... cookies, // 配置使用插件原型 $cookies }).$mount( '#app' ); // 第二種 import cookies from 'vue-cookies' // 導入插件 Vue.prototype.$cookies = cookies; // 直接配置插件原型 $cookies |
使用
1
2
3
4
5
6
7
8
9
|
// 增(改): key,value,exp(過期時間) // 1 = '1s' | '1m' | '1h' | '1d' this .$cookies.set( 'token' , token, '1y' ); // 查:key this .token = this .$cookies.get( 'token' ); // 刪:key this .$cookies.remove( 'token' ); |
注:cookie一般都是用來存儲token的
// 1) 什么是token:安全認證的字符串
// 2) 誰產生的:后臺產生
// 3) 誰來存儲:后臺存儲(session表、文件、內存緩存),前臺存儲(cookie)
// 4) 如何使用:服務器先生成反饋給前臺(登陸認證過程),前臺提交給后臺完成認證(需要登錄后的請求)
// 5) 前后臺分離項目:后臺生成token,返回給前臺 => 前臺自己存儲,發送攜帶token請求 => 后臺完成token校驗 => 后臺得到登陸用戶
(上面提到過用cookie也可以完成跨組件傳參,由于cookies中沒有設置默認值,所以如果是空值的話,在視圖函數中拿到的也是空,所以我們要在視圖函數中也設置一個默認值,然后對用cookies傳過來的值進行判斷如果不是空值的時候,用它替換默認值,然后渲染上去)
axios插件
安裝
1
|
>: cnpm install axios |
main.js配置
1
2
|
import axios from 'axios' // 導入插件 Vue.prototype.$axios = axios; // 直接配置插件原型 $axios |
使用
1
2
3
4
5
6
|
this .axios({ url: '請求接口' , method: 'get|post請求' , data: {post等提交的數據}, params: {get提交的數據} }).then(請求成功的回調函數). catch (請求失敗的回調函數) |
案例
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
|
// get請求 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請求 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) }); |
跨域問題(同源策略)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// 后臺接收到前臺的請求,可以接收前臺數據與請求信息,發現請求的信息不是自身服務器發來的請求,拒絕響應數據,這種情況稱之為 - 跨域問題(同源策略 CORS) // 導致跨域情況有三種 // 1) 端口不一致 // 2) IP不一致 // 3) 協議不一致 // Django如何解決 - django-cors-headers模塊 // 1) 安裝:pip3 install django-cors-headers // 2) 注冊: INSTALLED_APPS = [ ... 'corsheaders' ] // 3) 設置中間件: MIDDLEWARE = [ ... 'corsheaders.middleware.CorsMiddleware' ] // 4) 設置跨域: CORS_ORIGIN_ALLOW_ALL = True |
element-ui插件
安裝
1
|
>: cnpm i element-ui -S |
main.js配置
1
2
3
|
import ElementUI from 'element-ui' ; import 'element-ui/lib/theme-chalk/index.css' ; Vue.use(ElementUI); |
使用
依照官網 https://element.eleme.cn/#/zh-CN/component/installation api
總結
到此這篇關于vue中路由傳參以及跨組件傳參的文章就介紹到這了,更多相關vue路由及跨組件傳參內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/wangnanfei/p/11656714.html