vue路由,ajax,element-ui


復習

"""
1、vue項目環境:
	node => npm(cnpm) => vue/cli
2、vue項目創建:
	vue create 項目
	在pycharm中配置npm項目啟動
3、項目目錄
4、main.js:程序的入口文件
	加載vue環境
	加載插件環境:路由、倉庫、ajax、cookie、element-ui...
	加載自定義環境:全局樣式(global.css)、全局配置(settings.js)
	渲染根組件
5、.vue文件形式組件:
	template標簽:內部有且只有一個根標簽
	script標簽:export default {},導出該局部組件內容
	style標簽:scope屬性,實現樣式的組件化
6、項目運行的生命周期:main.js => router.js => 鏈接 => 頁面組件 => 替換根組件中的 router-view 標簽完成頁面渲染 => 通過 router-link | this.$router.push() 切換路由(鏈接) => 完成渲染組件的替換 => 頁面的跳轉
7、新建頁面的三步驟:
	1)創建頁面組件
	2)設置組件路由
	3)設置路由跳轉
8、組件的生命周期鈎子:組件從生成到銷毀整個過程的一些特殊時間節點回調的函數
9、this.$router路由跳轉 | this.$route路由數據 (this.$route.path)
"""

路由跳轉

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
routes: [
	// ...
    {
        path: '/course/:id/detail',
        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;
}

可以完成跨組件傳參的四種方式

// 1) localStorage:永久存儲數據
// 2) sessionStorage:臨時存儲數據(刷新頁面數據不重置,關閉再重新開啟標簽頁數據重置)
// 3) cookie:臨時或永久存儲數據(由過期時間決定)
// 4) vuex的倉庫(store.js):臨時存儲數據(刷新頁面數據重置)

vuex倉庫插件

store.js配置文件
export default new Vuex.Store({
    state: {
        title: '默認值'
    },
    mutations: {
        // mutations 為 state 中的屬性提供setter方法
        // setter方法名隨意,但是參數列表固定兩個:state, newValue
        setTitle(state, newValue) {
            state.title = newValue;
        }
    },
    actions: {}
})
在任意組件中給倉庫變量賦值
this.$store.state.title = 'newTitle'
this.$store.commit('setTitle', 'newTitle')
在任意組件中取倉庫變量的值
console.log(this.$store.state.title)

vue-cookies插件

安裝
>: 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
使用
// 增(改): 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校驗 => 后台得到登陸用戶

axios插件

安裝
>: 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) 注冊:
INSTALLED_APPS = [
	...
	'corsheaders'
]
// 3) 設置中間件:
MIDDLEWARE = [
	...
	'corsheaders.middleware.CorsMiddleware'
]
// 4) 設置跨域:
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);
使用
依照官網 https://element.eleme.cn/#/zh-CN/component/installation api


免責聲明!

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



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