vue中创建路由

每一个vue组件都有三部分组成
template:放html代码
script:放js相关
style:放css相关 vue中创建路由
1.先创建组件
Course.vue
2.router.js中导入组件,配置组件
import Course from './views/Course.vue' export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/course',
name: 'course',
component: Course
},
]
}) 3.app.vue中设置路由跳转
<template>
<div id="app">
<div id="nav">
<router-link to="/course">Course</router-link>
</div>
<router-view/>
</div>
</template>

组件中显示数据

<template>
<div class="course">
#插值表达式
<h1>{{name}}</h1>
<h1>{{age}}</h1>
</div>
</template>
<script>
export default {
name: 'course',
#返回数据
data: function () {
#通过return的方式
return {
name: '刘清正',
age: '18'
}
}
}
</script> #for循环显示数据
<template>
<div class="course">
<p v-for="item in course_list">{{item}}</p>
</div>
</template> <script> export default {
name: 'course',
data: function () {
return {
course_list:['python',"linux","java"]
}
}
}
</script>

通过axios与后台交互


配置axios
1.安装axios
npm install axios
2.在main.js中加入以下两句话
导入axios
import axios from 'axios'
放入全局变量中
Vue.prototype.$http = axios 使用axios
请求.request
回调函数 .then
异常捕捉 .catch
$http.request({
url:请求的地址
method:请求方式
}).then(function(response)){
#数据放在data中了
window.console.log(response.data)
}.catch(function(errors)){
window.console.log(errors)
} vue项目(前台)
<template>
<div class="course">
#需要写在course中
<button @click="foo">点我</button>
<p v-for="item in course_list">{{item}}</p>
</div>
</template> <script> export default {
name: 'course',
data: function () {
return {
course_list:[]
}
},
methods:{
foo:function () {
#直接在then的函数内使用this的话,this表示函数对象,
var _this = this;
this.$http.request({
//存在跨域问题
url:'http://127.0.0.1:8023/',
method:'get'
}).then(function (res) {
_this.course_list=res.data
}) }
}
}
</script> django项目(后台)
class Course(APIView):
def get(self,request,*args,**kwargs):
return Response(['python','linux','java'])

页面挂载完成,执行数据加载

上面的交互,不应该是点击后才显示,应该是点击此页面时就展示

vue项目(前台)
<template>
<div class="course">
<p v-for="item in course_list">{{item}}</p>
</div>
</template> <script>
export default {
name: 'course',
data: function () {
return {
course_list:[]
}
},
methods:{
foo:function () {
#直接在then的函数内使用this的话,this表示函数对象,
var _this = this;
this.$http.request({
//存在跨域问题
url:'http://127.0.0.1:8023/',
method:'get'
}).then(function (res) {
_this.course_list=res.data
})
}
},
#组件挂载
mounted:function(){
this.foo()
}
}
</script>

vue中使用element-ui

1.安装
npm i element-ui-S
2.在main.js中配置
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); 3.在http://element-cn.eleme.io/#/zh-CN/component/quickstart官网上copy组件来用

contentType组件

免费课程表course
id name time
学位课程表degreecourse
id name price
价格策略表
id period price obj_id table_id 1.免费课程表和学位表有不同的字段,故各建一张表
2.价格策略表需存放不同课程的价格策略,故需要table_id和obj_id两个字段来确认一条数据 class Course(models.Model):
name = models.CharField(max_lenth=32)
time = models.DateField(auto_now_add=True) class DegreeCourse(models.Model):
name = models.CharField(max_lenth=32)
teacter = models.CharField(max_lenth=32) class PricePolicy(models.Model):
period = models.IntegerField()
price = mmodels.DecimalField(max_digits=8,decimal_places=2)
obj_id = odels.IntegerField()
table_name = models.CharField(max_lenth=32) 以上创建类可以实现我们的需求
但是当我们进行以下查询时会很复杂
1.通过课程对象查询它所有的价格策略
2.查询所有价格策略的课程名称
3.给课程创建价格策略 故需要使用到django给我们提供的contentType组件
在我们进行数据库迁移时,django为我们创建了django_content_type表
里面是id对应我们的表名
#导入django的ContentType表
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation class Course(models.Model):
name = models.CharField(max_lenth=32)
time = models.DateField(auto_now_add=True)
#此字段方便了查询价格策略
policy = GenericRelation(to='PricePolicy') class DegreeCourse(models.Model):
name = models.CharField(max_lenth=32)
teacter = models.CharField(max_lenth=32)
policy = GenericRelation(to='PricePolicy') class PricePolicy(models.Model):
period = models.IntegerField()
price = mmodels.DecimalField(max_digits=8,decimal_places=2)
#必须交object_id和content_type因为源码默认传参就是这两个参数
object_id = odels.IntegerField()
#不删除记录,将此字段设置为空
content_type = models.ForeignKey(to=ContentType,null=True,on_delete=models.SET_NULL,db_constraint=False)
#此字段可以实现快速创建和查询课程
content_obj = GenericForeignKey('content_type','object_id')

测试类

from django.db import models

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation class Course(models.Model):
name = models.CharField(max_length=32)
section = models.IntegerField()
policy = GenericRelation('PricePolicy') class DegreeCourse(models.Model):
name = models.CharField(max_length=32)
author = models.CharField(max_length=32)
policy = GenericRelation('PricePolicy') class Lcourse(models.Model):
name = models.CharField(max_length=32)
teacher = models.CharField(max_length=32)
policy = GenericRelation('PricePolicy') class PricePolicy(models.Model):
price = models.DecimalField(max_digits=8,decimal_places=2)
period = models.IntegerField()
object_id = models.IntegerField()
content_type = models.ForeignKey(to= ContentType,null=True,on_delete=models.SET_NULL,db_constraint=False)
content_obj = GenericForeignKey()

测试代码

# 1.通过课程对象查询它所有的价格策略
course = Lcourse.objects.filter(pk=1).first()
policy_list = course.policy.all()
for policy in policy_list:
print(policy.price)
# 2.查询所有价格策略的课程名称
price_policy_list=PricePolicy.objects.all()
for price_policy in price_policy_list:
print(price_policy.content_obj.name)
# 3.给课程创建价格策略 course = Lcourse.objects.filter(pk=1).first()
PricePolicy.objects.create(price=3.3,period=2,content_obj=course)

vue创建路由,axios前后台交互,element-ui配置使用,django contentType组件的更多相关文章

  1. vue+element ui 的表格列使用组件

    前言:工作中用到 vue+element ui 的前端框架,有这个场景:很多表格的列有许多一样的,所以考虑将列封装为组件.转载请注明出处:https://www.cnblogs.com/yuxiaol ...

  2. vue-cli的项目中关于axios的全局配置,结合element UI,配置全局loading,header中做token传输

    在src目录中建立plugins文件夹,在文件夹内建立axios.js文件 "use strict"; import Vue from 'vue'; import axios fr ...

  3. Vue+element UI实现“回到顶部”按钮组件

    介绍 这是一个可以快速回到页面顶部的组件,当用户浏览到页面底部的时候,通过点击按钮,可快速回到页面顶部. 使用方法 由于该组件是基于element-UI进行二次封装的,所以在使用该组件时请务必安装el ...

  4. vue实现多语言国际化(vue-i18n),结合element ui、vue-router、echarts以及joint等。

    老板说我们的项目要和国际接轨,于是乎,加上了多语言(vue-i18n).项目用到的UI框架是element ui ,后续echarts.joint等全都得加上多语言. 一.言归正传,i18n在vue项 ...

  5. vue组件样式添加scoped属性之后,无法被父组件修改。或者无法在本组件修改element UI样式

    在vue开发中,需要使用scoped属性避免样式的全局干扰,但是这样在父组件中是无法被修改的,不仅如此如果项目中用了UI框架比如element Ui,这个时候在本组件也无法修改样式,因为权重问题.但是 ...

  6. vue2.0+Element UI 表格前端分页和后端分页

    之前写过一篇博客,当时对element ui框架还不太了解,分页组件用 html + css 自己写的,比较麻烦,而且只提到了后端分页 (见 https://www.cnblogs.com/zdd20 ...

  7. element UI Cascader 级联选择器 编辑 修改 数组 路径 问题(转载)

    来源:https://segmentfault.com/a/1190000014827485 element UI的Cascader级联选择器编辑时 vue.js element-ui 2 eleme ...

  8. vue简单路由(二)

    在实际项目中我们会碰到多层嵌套的组件组合而成,但是我们如何实现嵌套路由呢?因此我们需要在 VueRouter 的参数中使用 children 配置,这样就可以很好的实现路由嵌套. index.html ...

  9. element ui table(表格)点击一行展开

    element ui是一个非常不错的vue的UI框架,element对table进行了封装,简化了vue对表格的渲染. element ui表格中有一个功能是展开行,在2.0版本官网例子中,只可以点击 ...

随机推荐

  1. 如果查询的结果少,可以把查询结果当子集,再查另一张表,比join的速度快得多得多

    ) as qty from riv_print_history_l phl JOIN tab_user_profile up on up.UP_USER_ID = phl.PHL_PRINT_USER ...

  2. NET Core 2.0 微服务跨平台实践

    NET Core 2.0 微服务跨平台实践 相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和 ...

  3. C#入门笔记3 表达式及运算符2

    关系运算符,也称布尔比较运算符 注:var1为bool类型,var2与var3可以是其它类型.[数据类型看下一节] 运算符 类别 示例表达式 结果说明 == 二元 var1=var2==var3 如果 ...

  4. springboot使用schedule定时任务

    定时任务一般会存在中大型企业级项目中,为了减少服务器.数据库的压力往往会采用时间段性的去完成某些业务逻辑.比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回 ...

  5. 文件操作函数及光标,tell,truncate

    一丶文件基本操作 1.打开文件 open(文件名(路径),mode = "?",encoding = "字符集") 2.文件路径: 1.绝对路径,从磁盘的根目录 ...

  6. BaseAdapter.notifyDataSetChanged()之观察者设计模式及源码分析

    BaseAdapter.notifyDataSetChanged()的实现涉及到设计模式-观察者模式,详情请参考我之前的博文设计模式之观察者模式 Ok,回到notifyDataSetChanged进行 ...

  7. css隐藏元素的几种方法与区别

    css隐藏元素的几种方法与区别 一:display:none;隐藏不占位 display 除了不能加入 CSS3 动画豪华大餐之外,基本效果卓越,没什么让人诟病的地方. 二:position:abso ...

  8. 【虚拟机-网络IP】虚拟机配置静态 IP 以后无法连接的解决办法

    问题描述 将虚拟机内部 IP 地址从动态获取改成静态 IP 以后,远程连接失败. 问题分析 Azure 虚拟机的内部 IP 默认为动态分配, 由 DHCP 服务自动分配, 在虚拟机的生命周期内, 该 ...

  9. UVA 11987 Almost Union-Find (单点修改的并查集)

    此题最难处理的操作就是将一个单点改变集合,而普通的并查集是不支持这种操作的. 当结点p是叶子结点的时候,直接pa[p] = root(q)是可以的, p没有子结点,这个操作对其它结点不会造成任何影响, ...

  10. UVA 11997 K Smallest Sums (多路归并)

    从包含k个整数的k个数组中各选一个求和,在所有的和中选最小的k个值. 思路是多路归并,对于两个长度为k的有序表按一定顺序选两个数字组成和,(B表已经有序)会形成n个有序表 A1+B1<=A1+B ...