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. Codeforces Round #527 -A. Uniform String(思维)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  2. 064 Minimum Path Sum 最小路径和

    给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径.注意: 每次只能向下或者向右移动一步.示例 1:[[1,3,1], [1,5,1], [4,2,1]]根据 ...

  3. Ubuntu下安装nginx及使用

    首先介绍以下nginx.下图来自百科介绍:详细介绍地址:https://baike.baidu.com/item/nginx/3817705?fr=aladdin 在我们平时的开发娱乐中,也许并不会涉 ...

  4. 机器学习框架ML.NET学习笔记【7】人物图片颜值判断

    一.概述 这次要解决的问题是输入一张照片,输出人物的颜值数据. 学习样本来源于华南理工大学发布的SCUT-FBP5500数据集,数据集包括 5500 人,每人按颜值魅力打分,分值在 1 到 5 分之间 ...

  5. 笔试题五道spring

    项目中如何体现Sping 中的切面编程,举例说明 面向切面编程:主要是横切 一个关注点,将一个关注点模块化成一个切面,在切面上声明一个通知(Advice)和切入点 (Pointcut) 通知:是指在切 ...

  6. VS2012快捷键消失

    我也是网上搜的不过我认为挺有效就自己摘录下来了,具体原作者也找不到,所以就下手了,望原谅. 开始菜单 -->所有程序-->Visual Studio 2012文件夹 --> Visu ...

  7. JavaWeb项目开发中eclipse缓存问题

    学习Java快2年了 有时候改完代码启动tomcat测试时,新代码不生效,这可能就是缓存问题. 所以平时就用以下几个方法解决,如果还是解决不了,就找老师吧! 1.清理项目 2.移除项目,清理tomca ...

  8. 【Java】JMM内存模型和JVM内存结构

    JMM内存模型和JVM内存结构 JAVA内存模型(Java Memory Model) Java内存模型,一般指的是JDK 5 开始使用的新的内存模型,主要由JSR-133: JavaTM Memor ...

  9. 洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace

    坏掉的项链Broken Necklace 难度:★ Code: #include <iostream> #include <cstdio> #include <cstri ...

  10. Python+selenium之获取验证信息

    通常获取验证信息用得最多的几种验证信息分别是title,URL和text.text方法用于获取标签对之间的文本信息. 代码如下: from selenium import webdriverimpor ...