Vue框架(四)——路由跳轉、路由傳參、cookies、axios、跨域問題、element-ui模塊


路由跳轉

三種方式:

$router.push / $router.go / router-link to

this.$router.push('/course');
this.$router.push({name: course});  //這個name是router.js里面設置的name
this.$router.go(-1);  //頁面后退
this.$router.go(1);   //前進 <router-link to="/course">課程頁</router-link>
<router-link :to="{name: 'course'}">課程頁</router-link>   

路由傳參

第一種:

router.js設置

routes: [
    // ...
    {
        path: '/course/:id/detail',     //:id接收參數
        name: 'course-detail',
        component: CourseDetail
    },
]

跳轉  .vue

<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

created() {
    let id = this.$route.params.id;
}

第二種:

router.js設置

routes: [
    // ...
    {
        path: '/course/detail',
        name: 'course-detail',
        component: CourseDetail
    },
]

跳轉 .vue

<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

created() {
    let id = this.$route.query.id;
}

倉庫:vuex

倉庫配置:store.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
    // 全局可以訪問的變量 - 獲取值
    // 組件內:this.$store.state.title
    state: {
        title: '主頁'
    },
    // 全局可以訪問的方法 - 修改值
    // 組件內:this.$store.commit('updateTitle', '新值')
    mutations: {
        updateTitle (state, newValue) {
            state.title = newValue
        }
    },
    actions: {}
})

賦值
this.$store.state.title = 'newTitle'
this.$store.commit('setTitle', 'newTitle')

 
         

取值
console.log(this.$store.state.title)

vue-cookies插件  (設置cookies可以使用)

安裝:在pycharm上面安裝

cnpm install vue-cookies

main.js配置   (推薦使用第二種設置)

// 第一種
import cookies from 'vue-cookies'      // 導入插件
Vue.use(cookies);                    // 加載插件
new Vue({
    // ...
    cookies,                        // 配置使用插件原型 $cookies
}).$mount('#app');

// 第二種
import cookies from 'vue-cookies'    // 導入插件
Vue.prototype.$cookies = cookies;    // 直接配置插件原型 $cookies

使用  (增刪改查cookies)

// 增(改): 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');

注意:cookies一般都是用來存儲token的

// 1) 什么是token:安全認證的字符串
// 2) 誰產生的:后台產生
// 3) 誰來存儲:后台存儲(session表、文件、內存緩存),前台存儲(cookie)
// 4) 如何使用:服務器先生成反饋給前台(登陸認證過程),前台提交給后台完成認證(需要登錄后的請求)
// 5) 前后台分離項目:后台生成token,返回給前台 => 前台自己存儲,發送攜帶token請求 => 后台完成token校驗 => 后台得到登陸用戶

前后台交互:axios插件

這個類似之前講的ajax

安裝:也是在pycharm中安裝

cnpm install axios

main.js配置

import axios from 'axios'    // 導入插件
Vue.prototype.$axios = axios;    // 直接配置插件原型 $axios

使用

this.$axios({
    url: '請求接口',
    method: 'get|post請求',
    data: {post等提交的數據},
    params: {get提交的數據}
}).then(請求成功的回調函數).catch(請求失敗的回調函數)

//舉例
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)
});

跨域問題(同源策略)

產生跨域問題的原因

// 后台接收到前台的請求,可以接收前台數據與請求信息,發現請求的信息不是自身服務器發來的請求,拒絕響應數據,這種情況稱之為 - 跨域問題(同源策略 CORS)

// 導致跨域情況有三種
// 1) 端口不一致
// 2) IP不一致
// 3) 協議不一致

解決方法在django項目中下載django-cors-headers模塊

// 1) 安裝:pip3 install django-cors-headers
// 2) 注冊:在settings.py文件
INSTALLED_APPS = [
    ...
    'corsheaders'
]
// 3) 設置中間件:
MIDDLEWARE = [
    ...
    'corsheaders.middleware.CorsMiddleware'
]
// 4) 設置跨域:
CORS_ORIGIN_ALLOW_ALL = True

element-ui插件   類似於bootstrap往里面找各種樣式

安裝

cnpm i element-ui -S

main.js配置

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  復制粘貼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM