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的更多相关文章

随机推荐

  1. CSP-S 2019初赛前后小记

    Preface 做完了初赛前的最后一场模拟赛,虽然这场做的挺好.但由于之前的一场难度很高,再加上自己犯了一些ZZ错误因此对初赛也有了一些不安之情 想起去年自己对于初赛的态度,实在是愧疚虽然去年文化课弃 ...

  2. vue项目里面使用脚手架实现跨域

    今天在做vue项目的时候,项目在本地,接口数据在阿里云,这就造成了跨域,在网上找了好久,网上大部分的方法都是找到config文件夹下面的index进行修改的,可是我找到的Index却和他们描述的不一样 ...

  3. Python的互斥锁与信号量

    并发与锁 a. 多个线程共享数据的时候,如果数据不进行保护,那么可能出现数据不一致现象,使用锁,信号量.条件锁 b. c.互斥锁1. 互斥锁,是使用一把锁把代码保护起来,以牺牲性能换取代码的安全性,那 ...

  4. Python之基本运算符

    基本运算符 1.算符运算符 运算符 描述 例子 + 两个对象相加 a+b - 两个对象相减 a-b * 两个数相乘或返回一个被重复若干次的字符串 a*b / 两个数相除 a/b % 取模,返回除法的余 ...

  5. 【linux】dmesg命令显示开机信息和设备加载情况

    Linux命令dmesg用来显示开机信息,kernel会将开机信息存储在ring buffer中.您若是开机时来不及查看信息,可利用dmesg来查看.开机信息亦保存在/var/log目录中,名称为dm ...

  6. WPF中Button的背景图片,实现禁止IsMouseOver时显示默认

    <Button x:Name="btnPickUpNum" Click="PickUpNum_OnClick" Grid.Row="1" ...

  7. Springboot vue 前后分离 跨域 Activiti6 工作流 集成代码生成器 shiro权限

    官网:www.fhadmin.org 特别注意: Springboot 工作流  前后分离 + 跨域 版本 (权限控制到菜单和按钮) 后台框架:springboot2.1.2+ activiti6.0 ...

  8. JS基础语法---练习:交换两个变量的值

    * JavaScript简称为JS * JavaScript是什么?     * 是一门脚本语言:不需要编译,直接运行     * 是一门解释性的语言:遇到一样代码就解释一行代码     * C#语言 ...

  9. ucoreOS_lab 1~8 实验报告导航

    所有的实验已经全部完成,实验的源代码及报告都在 Github 上,欢迎大家批评指正,如果觉得对你有帮助的话,欢迎为此项目 star & watch & fork 三连,让更多的朋友们看 ...

  10. 版本管理·玩转git(分支管理)

    在开发中,遇到这样的情况怎么办? 网站已有支付宝在线支付功能,要添加"微信支付",修改了两个文件,wechat.php.pay.php. 刚做到一半,突然有个紧急bug:支付宝支付 ...