1.路由-页面跳转
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>
代码:
1 <template> 2 <div class="home"> 3 <Nav/> 4 <!--逻辑跳转页面--> 5 <div class="router"> 6 <button type="button" @click="goPage('/course')">课程页</button> 7 <button type="button" @click="goHome('/')">主页</button> 8 <button type="button" @click="goBack">返回上一页</button> 9 <!--通过名字来跳转--> 10 <button type="button" @click="goPageName('/course')">课程页(name)</button> 11 12 <!--怎么把这个也通过名字跳转--> 13 <!--<router-link to="/course">课程页(name)</router-link>--> 14 <!--直接去找这个路由的名字--> 15 <router-link :to="{name:'course'}">课程页(name)</router-link> 16 </div> 17 18 </div> 19 </template> 20 21 <script> 22 import Nav from '@/components/Nav.vue' 23 export default { 24 name: 'home', 25 components: { 26 Nav 27 }, 28 methods:{ 29 goPage(page){ 30 //直接写死去哪个页面 31 let currentPage = this.$route.path; 32 // 如果你在主页,你还点击主页,就会报错,需要做一下判断 33 if (currentPage !== page){ 34 this.$router.push(page) 35 } 36 37 }, 38 goBack(){ 39 // js逻辑使用history,完成返回上一页 40 this.$router.go(-1) 41 42 //返回上2页 43 // this.$router.go(-2) 44 45 // 前进一页 46 // this.$router.go(1) 47 }, 48 // 通过名字跳转 49 goPageName(pageName) { 50 this.$router.push({ 51 name:pageName 52 }) 53 } 54 55 } 56 } 57 </script>
2.课程与详情页简单搭建
小组件:
components,CourseCard 文件夹里面
代码:
1 <template> 2 <!--写一个个课程--> 3 <div class="course-card"> 4 <!--添加一个点击事件--> 5 <h1 @click="goDetail">{{ course.name }}</h1> 6 7 </div> 8 </template> 9 10 <script> 11 export default { 12 name: "CourseCard", 13 // 这是从页面组件那边穿过的,这个是父传子 14 props:['course'], 15 // 点击事件 16 methods:{ 17 goDetail(){ 18 this.$router.push({ 19 name:'course-detail' 20 }) 21 } 22 } 23 } 24 </script> 25 26 <style scoped> 27 /*写一些样式*/ 28 .course-card h1 { 29 width: 200px; 30 height: 200px; 31 border-radius: 50%; 32 background-color: coral; 33 font: normal 20px/200px 'STSong'; 34 float: left; 35 text-align: center; 36 cursor:pointer; 37 } 38 </style>
页面组件里面的Course文件夹 课程页:
代码:
1 <template> 2 <div class="course"> 3 <Nav /> 4 5 <!--在这渲染页面 :key="course.name" 这个是建立一下缓存的 :course="course" 父传子--> 6 <div class="main"> 7 <CourseCard v-for="course in course_list" :key="course.name" :course="course"/> 8 </div> 9 10 </div> 11 </template> 12 13 <!--三步骤: 14 导入 渲染 注册 15 16 --> 17 <script> 18 import Nav from '@/components/Nav' 19 import CourseCard from '@/components/CourseCard' 20 // 现在可以在这直接制造课程 21 let course_list = [ 22 { 23 id:1, 24 name:'Python入门到入土' 25 }, 26 { 27 id:2, 28 name:'前端放弃攻略' 29 }, 30 { 31 id:3, 32 name:'vue' 33 }, 34 { 35 id:4, 36 name:'基佬是怎样炼成的' 37 }, 38 39 40 41 42 ]; 43 44 export default { 45 name: "Course", 46 components:{ 47 Nav, 48 CourseCard, 49 }, 50 data(){ 51 return { 52 course_list, 53 } 54 } 55 } 56 </script> 57 58 <style scoped> 59 60 </style>
CourseDetail文件夹 详情页:
代码:
1 <template> 2 <!--详情页--> 3 <div class="course-detail"> 4 <h1>Python入门到入土</h1> 5 <p>三分钟入门,一分钟入土,一定要学啊</p> 6 7 </div> 8 </template> 9 10 <script> 11 12 export default { 13 name: "CourseDetail" 14 } 15 </script> 16 17 <style scoped> 18 19 </style>
router 路由文件夹:
扫描二维码关注公众号,回复:
7458612 查看本文章
代码:
1 import Vue from 'vue' 2 import Router from 'vue-router' 3 import Home from './views/Home.vue' 4 import Course from './views/Course.vue' 5 import CourseDetail from './views/CourseDetail.vue' 6 7 Vue.use(Router); 8 9 export default new Router({ 10 mode: 'history', 11 base: process.env.BASE_URL, 12 routes: [ 13 { 14 path: '/', 15 name: 'home', 16 component: Home 17 }, 18 { 19 path: '/course', 20 name: 'course', 21 component: Course 22 }, 23 { 24 path: '/course/detail', 25 name: 'course-detail', 26 component: CourseDetail 27 }, 28 29 30 ] 31 })
3.路由-俩种传参
第一种:
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;
}
详细代码:
写一个个的课程小组件
CourseCard.vue
1 <template> 2 <!--写一个个课程--> 3 <div class="course-card"> 4 <!--添加一个点击事件--> 5 <!--这是通过逻辑跳转的--> 6 <!--<h1 @click="goDetail">{{ course.name }}<h1/>--> 7 <!--第三种--> 8 <router-link :to="`/course/${course.id}/detail`">{{ course.name }}</router-link> 9 <!--第二种--> 10 <!--<router-link :to="{--> 11 <!-- name: 'course-detail',--> 12 <!-- query: {id: course.id}--> 13 <!--}">{{ course.name }}</router-link>--> 14 15 </div> 16 </template> 17 18 <script> 19 export default { 20 name: "CourseCard", 21 // 这是从页面组件那边穿过的,这个是父传子 22 props:['course'], 23 // 点击事件 24 methods:{ 25 goDetail(){ 26 // this.$router.push({ 27 // name:'course-detail' 28 // }); 29 // 第一种传参方式,因为涉及到路由跳转所以用router 30 // this.$router.push({ 31 // name:'course-detail', 32 // // 传参用params字典的方式传 33 // params:{ 34 // id:this.course.id, 35 // } 36 // }); 37 // 第二种传参方式 38 // this.$router.push({ 39 // name:'course-detail', 40 // query:{ 41 // id:this.course.id, 42 // } 43 // }); 44 45 // 第三种方式 建议使用这一种 46 this.$router.push(`/course/${this.course.id}/detail/`) 47 48 49 50 51 } 52 53 } 54 } 55 </script> 56 57 <style scoped> 58 /*写一些样式*/ 59 .course-card h1,.course-card a { 60 width: 200px; 61 height: 200px; 62 border-radius: 50%; 63 background-color: coral; 64 font: normal 20px/200px 'STSong'; 65 float: left; 66 text-align: center; 67 cursor:pointer; 68 display: block; 69 } 70 </style>
页面课程详情页组件
CourseDetail.vue
1 <template> 2 <!--详情页--> 3 <div class="course-detail"> 4 <h1>{{ course.name }}</h1> 5 <p>{{ course.info }}</p> 6 <p>{{ course.price }}</p> 7 8 9 </div> 10 </template> 11 12 <script> 13 // 详情页也应该有数据,是详细的数据 14 let course_list = [ 15 { 16 id:1, 17 name:'Python入门到入土', 18 price:6.6, 19 info:'三分钟入门,一分钟入土,一定要学啊', 20 }, 21 { 22 id:2, 23 name:'前端放弃攻略', 24 price:5.5, 25 info:'学的欲仙欲死', 26 }, 27 { 28 id:3, 29 name:'vue', 30 price:9.9, 31 info:'vue前端框架' 32 }, 33 { 34 id:4, 35 name:'基佬是怎样炼成的', 36 price:8.8, 37 info:'这本书你值得拥有', 38 }, 39 ]; 40 export default { 41 name: "CourseDetail", 42 data(){ 43 return { 44 // 课程初始为空的 45 course:{} 46 } 47 }, 48 // 拿到课程页点击的id页课程详情的的id进行匹配,然后渲染出相应的数据 49 // for of 遍历的是值 for in遍历的是取值的依据(arr是索引,obj是key) 50 created() { 51 // 这里现在就不能写死了,需要点谁就渲染谁的详情 52 //params没有就从query里面去拿 53 let id = this.$route.params.id || this.$route.query.id || 1; 54 for (let course of course_list) { 55 if (id === course.id) { 56 this.course = course; 57 break 58 } 59 } 60 // for (let index in course_list) { 61 // console.log(index) 62 // } 63 64 } 65 66 } 67 </script> 68 69 <style scoped> 70 71 </style>
4.跨组件传参-vuex仓库
可以完成跨组件传参的四种方式
// 1) localStorage:永久存储数据
// 2) sessionStorage:临时存储数据(刷新页面数据不重置,关闭再重新开启标签页数据重置)
// 3) cookie:临时或永久存储数据(由过期时间决定)
// 4) vuex的仓库(store.js):临时存储数据(刷新页面数据重置)
vue仓库插件
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)
5.vue的cookie操作(vue-cookie插件)
安装:
>: 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校验 => 后台得到登陆用户
6.vue的ajax操作
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(请求失败的回调函数)
案例:
1 // get请求 2 this.$axios({ 3 url: 'http://127.0.0.1:8000/test/ajax/', 4 method: 'get', 5 params: { 6 username: this.username 7 } 8 }).then(function (response) { 9 console.log(response) 10 }).catch(function (error) { 11 console.log(error) 12 }); 13 14 // post请求 15 this.$axios({ 16 url: 'http://127.0.0.1:8000/test/ajax/', 17 method: 'post', 18 data: { 19 username: this.username 20 } 21 }).then(function (response) { 22 console.log(response) 23 }).catch(function (error) { 24 console.log(error) 25 });
7.跨域问题(同源策略)
// 后台接收到前台的请求,可以接收前台数据与请求信息,发现请求的信息不是自身服务器发来的请求,拒绝响应数据,这种情况称之为 - 跨域问题(同源策略 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
8.vue的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