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的更多相关文章
随机推荐
- 打包一个python解释器
利用python的exec语句,可以很方便地动态执行python语句.如果一个python代码打包为了exe,其原先的代码就很难更改了.一个好的解决方法就是import相应的库,然后把主程序段放到一个 ...
- 第04组 Beta冲刺(3/5)
队名:new game 组长博客 作业博客 组员情况 鲍子涵(队长) 过去两天完成了哪些任务 整理素材 接下来的计划 素材和脚本相连 引入声音素材 还剩下哪些任务 让游戏本体运行 遇到了哪些困难 时间 ...
- Linux 学习记录一(安装、基本文件操作).
Linux 名字的由来,是当时作者将初版的 Linux 发布在网上,供别人下载完善,而那个核心文件夹就叫 Linux,就这么叫着了.而为什么 Linux 的吉祥物是一只企鹅呢?是因为当时大家要发行稳定 ...
- 基于python的selenium常用操作方法(1)
1 selenium定位方法 Selenium提供了8种定位方式. · id · name · class name · tag ...
- oracle中decode函数用法及应用
用法 1.decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 相当于if else 那种方式. 2.decode(字段或字段的运算,值1,值2,值3) 当字段或字段的运 ...
- Maven的assembly插件在linux启动卡住Starting the localhost.localdomain
1.今天在测试assembly的时候,在Linux虚拟机,内存配置为512mb,然后开始在Linux上运行assembly的时候就会一直卡住 2.停止运行后,查看了下日志 [root@localho ...
- .net core入门-项目启动时报错:HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure
在打开Core的项目首页时,页面有时候会出现:HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure的错误,但是这里面看不出任何错误详情,这个时候 ...
- go-GUI-代码
直接看网址吧,所有的GO-GUI代码!~~~~ 网址
- Linux网络——修改配置文件
Linux网络——修改配置文件 摘要:本文主要学习了如何通过修改配置文件来设置网络参数. 配置文件 通过修改系统的配置文件为系统设置网络参数,这种方式的优点是可以永久保存,计算机重启后仍然生效.缺点是 ...
- vuepress1.x入门使用
要点: 1.用npm操作会有各种问题,用yarn取代之; 2.yarn可以用npm全局安装,而npm是node环境自带,node环境去官网下载安装; 3.没有必要全局安装vuepress 操作: 1. ...