vue_day05
vue前后端交互:
vue 分离前后端交互:
后端处理跨域 (CORS问题):
- 跨域问题??? 接收请求,不响应(同源策略)
同源策略: http 协议 ,ip / 服务器地址相同 (app应用端口相同)
跨域:协议、ip地址、应用端口有一个不同,就是跨域
django 默认只对同源策略 响应
跨域问题解决:
django : pip3 install django-cors-headers
插件参考地址:https://github.com/ottoyiu/django-cors-headers/
项目配置:
python-- settings.py
# 注册app
INSTALLED_APPS = [
...
'corsheaders'
]
# 添加中间件
MIDDLEWARE = [
...
'corsheaders.middleware.CorsMiddleware'
]
# 允许跨域源
CORS_ORIGIN_ALLOW_ALL = True
vue前端发送请求:
eg:
<script>
export default {
name: "CarDetail",
data() {
return {
car: {}
}
},
created() {
// 拿到路由传递来的car主键
let pk = this.$route.query.pk || this.$route.params.pk;
// 主键不存在,就直接结束方法
if (!pk) return false;
// 主键存在才请求后台
this.$axios({
url: this.$settings.base_url + `/car/${pk}/`,
}).then(response => {
this.car = response.data
}).catch(error => {
console.log(error.response)
})
}
}
</script>
this.axios({
url: '请求接口',
method: 'get|post请求',
data: {post等提交的数据},
params: {get提交的数据}
}).then(请求成功的回调函数).catch(请求失败的回调函数)
vue请求插件--axios:
三部曲:
1.安装( --》安装在项目目录下)
--》cnpm install axios
2.配置环境 (main.js)
import axios from 'axios'
Vue.prototype.$axios = axios
3.使用方式(组件的逻辑中发送ajax请求):
this.$axios({
url: 'http://127.0.0.1:8000/cars/',
method: 'get',
}).then(response => {
console.log(response);
// this.cars = response.data;
}).catch(error => { // 网络状态码为4xx、5xx
console.log(error);
main.js配置:
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
Vue.config.productionTip = false;
// 全局css
require('@/assets/css/global.css');
// 全局js
import settings from '@/assets/js/settings'
Vue.prototype.$settings = settings;
// 配置axios
import axios from 'axios'
Vue.prototype.$axios = axios; //定义vue实列的属性
// 配置element-ui--》vue文件样式模板
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
// 配置bootstrap,前提要配置jQuery
import "bootstrap"
import "bootstrap/dist/css/bootstrap.css"
前端朝后端请求传参方式:
<script>
import Nav from '@/components/Nav'
import CarTag from '@/components/CarTag'
export default {
name: "Car",
components: {
Nav,
CarTag,
},
data() {
return {
cars: []
}
},
created() {
// 完成ajax请求后台,获得数据库中的数据
this.$axios({
url: this.$settings.base_url + '/cars/',
method: 'post',
params: { // url拼接参数:任何请求都可以携带
a: 1,
b: 2,
},
data: { // 数据包参数:get请求是无法携带的
x: 10,
y: 20,
}
}).then(response => {
// console.log('正常', response);
this.cars = response.data;
}).catch(error => { // 网络状态码为4xx、5xx
console.log('异常', error.response);
});
}
}
</script>
数据传参数方式:
拼接参数:post (后端获取-->body内 (没有解析的数据))
data : {
// --》post 请求参数
}
数据包参数: get 后台: request.GET 获取参数
params: {
a: 1,
}
get 没有安全认证 (速度快)
app 注册--》 反射关联()
django后端返回数据样式:
#样式一:
def get_cars(request, *args, **kwargs):
car_query = models.Car.objects.values('id', 'title', 'img')
car_list = list(car_query)
for car in car_list:
car['img'] = '%s%s%s' % ('http://localhost:8000', settings.MEDIA_URL, str(car.get('img')))
return JsonResponse(car_list, safe=False)
#样式二:
def get_car(request, *args, **kwargs):
pk = kwargs.get('pk')
car_obj = models.Car.objects.filter(pk=pk).values('id', 'title', 'img', 'price', 'info').first()
if car_obj:
car_obj['img'] = '%s%s%s' % ('http://localhost:8000', settings.MEDIA_URL, str(car_obj.get('img')))
return JsonResponse(car_obj, json_dumps_params={'ensure_ascii': False})
return JsonResponse({'msg': '数据不存在'})
vue配置ElementUI:
ElementUI介绍:
一套为开发者、设计师和产品经理准备的基于 Vue 2.0 的桌面端组件库 (类似于bootstarp 样式组件 (本身就是vue组件))
----》https://element.eleme.cn/#/zh-CN/component/installation 官网地址
三部曲:
1.安装插件:(在该项目下)
cnpm install element-ui
2.配置环境:
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
3.使用:
直接选定需要的样式模板 (复制粘贴----》在进行修改)
Vue配置jq+bs:
jquery :
cnpm install jquery
配置jQuery:在vue.config.js中配置 (创建配置文件)
const webpack = require("webpack");
module.exports = {
configureWebpack: {
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
#"window.jQuery": "jquery",
"window.$": "jquery",
Popper: ["popper.js", "default"]
})
]
}
};
使用: $ ; 单个$ 表示特殊字符(jquery)
bootstarp:
cnpm install bootstrap@3
配置BootStrap:在main.js中配置
import "bootstrap"
import "bootstrap/dist/css/bootstrap.css"
vue_day05的更多相关文章
随机推荐
- javascript中优雅的处理async和await异常
let handler = async function(needErr) { return new Promise((resolve, reject) => { if (needErr) { ...
- CentOS7下rsync服务端与Windows下cwRsync客户端实现数据同步配置方法
最近需求想定期备份服务器d盘的数据到Linux服务器上面,做个笔记顺便写下遇到的问题 以前整过一个win下的cwrsync(客户端)+rsync(服务端:存储)的bat脚本 和整过一个Linux下的r ...
- SLB外部端口非80时---》转发到nginx---》URL跳转丢失端口的解决方案
配置nginx反向代理时遇到一个问题,当设置nginx监听80端口时转发请求没有问题.但一旦设置为监听其他端口,就一直跳转不正常: 如,访问欢迎页面时应该是重定向到登录页面,在这个重定向的过程中端口丢 ...
- linux服务器上配置进行kaggle比赛的深度学习tensorflow keras环境详细教程
本文首发于个人博客https://kezunlin.me/post/6b505d27/,欢迎阅读最新内容! full guide tutorial to install and configure d ...
- PVANET: Deep but Lightweight Neural Networks for Real-time Object Detection
URL: https://arxiv.org/abs/1608.08021 year: 2016 TL;DR PVANet 一个轻量级多物体目标检测架构, 遵循 "less channels ...
- 五分钟搞定 HTTPS 配置,二哥手把手教
01.关于 FreeSSL.cn FreeSSL.cn 是一个免费提供 HTTPS 证书申请.HTTPS 证书管理和 HTTPS 证书到期提醒服务的网站,旨在推进 HTTPS 证书的普及与应用,简化证 ...
- 2、Hibernate持久化编写
一.对于hibernate中的PO编写规则: 1. 必须提供一个无参数的public构造方法 2. 所有属性要private ,对外提供public 的get/set方法 3. 在PO类必须提 ...
- antV G2 为柱状图添加背景颜色
工作中需要在基础柱状图的基础上添加一个自定义高度的背景颜色, 基础柱状图: 目标柱状图: 由于chart绘图可以重叠,通过该特性,我们可以在画两次图重叠在一起,第一次绘图描述背景,第二次绘图描述数据, ...
- .net core程序强制以管理员权限启动
当我们编写windows程序的时候,很多时候需要程序默认以管理员权限运行,以前在.net 程序中直接新建一个app.manifest,设置requestedExecutionLevel 节点即可 &l ...
- 二叉查找树的实现与讲解(C++)
注:这篇文章源于:https://mp.csdn.net/postedit/99710904, 无需怀疑抄袭,同一个作者,这是我在博客园的账号. 在二叉树中,有两种非常重要的条件,分别是两类数据结构的 ...