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 复制粘贴
Vue框架(四)——路由跳转、路由传参、cookies、axios、跨域问题、element-ui模块的更多相关文章
- vue 路由跳转,传参
一.直接跳转 //js1.this.$router.push('/ad_new') //html 2.<router-link to="/ad_check"> < ...
- vue 和 react 路由跳转和传参
react 1 .跳转方式加传参 this.props.history.push({ //地址 pathname: '/film/Details', //路由传参 ...
- Angular:路由的配置、传参以及跳转
①路由的配置 1.首先用脚手架新建一个项目,在路由配置时选择yes 2.用ng g component创建组件 3.在src/app/app-routing.module.ts中配置路由 import ...
- (day68)Vue-CLI项目、页面跳转和传参、生命周期钩子
目录 一.Vue-CLI (一)环境搭建 (二)项目的创建 (三)项目目录结构 (四)Vue组件(.vue文件) (五)全局脚本文件main.js(项目入口) (六)Vue请求生命周期 二.页面跳转和 ...
- flutter页面间跳转和传参-Navigator的使用
flutter页面间跳转和传参-Navigator的使用 概述 flutter中的默认导航分成两种,一种是命名的路由,一种是构建路由. 命名路由 这种路由需要一开始现在创建App的时候定义 new M ...
- spring mvc controller间跳转 重定向 传参(转)
spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...
- JQuery 绑定select标签的onchange事件,弹出选择的值,并实现跳转、传参
<script src="jquery.min.js" type="text/javascript"></script> <scr ...
- app之间的跳转和传参问题
app 之间跳转和传参: 首先 创建2个app formApp (需要跳转到另外app的项目) toApp(被跳转的项目) 一:在toApp 项目中的操作: 1:创建URLSchemes ...
- 微信小程序开发:学习笔记[8]——页面跳转及传参
微信小程序开发:学习笔记[8]——页面跳转及传参 页面跳转 一个小程序拥有多个页面,我们可以通过wx.navigateTo推入一个新的页面.在首页使用2次wx.navigateTo后,页面层级会有三层 ...
- scrapy框架的日志等级和请求传参, 优化效率
目录 scrapy框架的日志等级和请求传参, 优化效率 Scrapy的日志等级 请求传参 如何提高scripy的爬取效率 scrapy框架的日志等级和请求传参, 优化效率 Scrapy的日志等级 在使 ...
随机推荐
- AsyncAPI 几个方便的工具
AsyncAPI 类似openapi 也提供了一些周边工具 editor 类似openapi 的,但是目前官方已经不推荐使用了,推荐使用playground,以下是构建一个容器镜像 clone 代码 ...
- CENTOS 7 下安装 REDIS 5.0.6 完整步骤
第一步:下载redis安装包 wget http://download.redis.io/releases/redis-5.0.6.tar.gz 第二步:解压压缩包 tar -zxvf redis ...
- [RN] React Native中使用 react-native-scrollable-tab-view嵌套在ScrollView里,导致 子内容 在安卓上无法显示
React Native中使用 react-native-scrollable-tab-view嵌套在ScrollView里,导致 子内容 在安卓上无法显示 问题: 0.9.0 或 0.8.0 版本的 ...
- 洛谷/SPOJ SP3267 题解
若想要深入学习主席树,传送门. Description: 给定数列 \(\{a_n\}\) ,求闭区间 \([l,r]\) 的互异的个数. Method: 扫描序列建立可持续化线段树,若此元素是第一次 ...
- JPA批量插入优化
遇到一个需求是excel数据导入,一次大概会批量插入几万的数据.写完一测奇慢无比. 于是开始打日志,分析代码,发现是插入数据库的时候耗时很长,发现是spring data jpa的原因. 翻看jpa的 ...
- Fluent Meshing分离边界层网格
源视频链接: https://pan.baidu.com/s/1SYB7UdRuXOGYXYwmxKADdw 提取码: h7qj
- Xshell远程登录
1.xshell由一台服务器a登录另一台服务器b sftp -oPort = root@ip 2.下载git /上传pull git b服务器想下载的目标文件目录 a服务器上的文件下载目录
- 为什么“四年一闰、百年不闰、四百年又闰”及 判断闰年的方法
小学数学学习方法: 为什么"四年一闰.百年不闰.四百年又闰"及判断平年和闰年的妙法 三年级的小朋友们都知道:我们公历年份是4的倍数的那一年一般都是闰年,但公历年份是100的倍数时, ...
- 美化自己的loading框
美化自己的loading框 ._showloading { position: absolute; left:; width: 100%; height: 100%; top:; background ...
- Mysql模糊查询like提速优化
LOCATE('substr',str,pos)方法 SELECT LOCATE('xbar',`foobar`); ###返回0 SELECT LOCATE('bar',`foobarbar`); ...