目录
表单控制
input:checkbox(单选,多选),radio(单选)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="app">
<h1>表单控制</h1>
<p>用户名:<input type="text" v-model="name"></p>
<p>密码:<input type="text" v-model="password"></p>
<p><input type="checkbox" v-model="isRemember">记住密码</p>
<p>
<input type="radio" v-model="gender" value="1"> 男
<input type="radio" v-model="gender" value="2"> 女
<input type="radio" v-model="gender" value="0"> 其他
</p>
<p>
爱好:
<input type="checkbox" value="basketball" v-model="hobby"> basketball
<input type="checkbox" value="football" v-model="hobby"> football
<input type="checkbox" value="pingpong" v-model="hobby"> pingpong
<input type="checkbox" value="dancing" v-model="hobby"> dancing
</p>
{{hobby}}
<button @click="funcSubmit">提交</button>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
name: '',
password: '',
isRemember: false,
gender: '',
hobby: []
},
methods: {
funcSubmit() {
console.log(this.gender)
}
}
})
</script>
</html>
基本购物车案例
<script>src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
var goodList = [
{id: 1, name: '钱包', price: 1200, count: 1},
{id: 1, name: '挎包', price: 2000, count: 2},
{id: 1, name: '书包', price: 120, count: 5},
{id: 1, name: '皮包', price: 12000, count: 3},
{id: 1, name: '面包', price: 12, count: 11},
]
# 方式一:js的基于索引的循环
for (var i = 0; i < goodList.length; i++) {
console.log(goodList[i])
}
# 方式二:基于迭代的循环
for (i in goodList) {
console.log(goodList[i])
}
# 方式三:of循环,基于迭代
for (i of goodList) {
console.log(i)
}
# 方式四:数组的循环方法
goodList.forEach(item => {
console.log('-----', item)
})
# 方式五:引入jquery
$.each(goodList, (i, v) => {
console.log(v)
})
购物车进阶版
1.展示购物车
2.全选所有商品
3.加减商品数量
4.计算选中商品的总价
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./js/vue.js"></script>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div id="app">
<div class="container-fluid">
<h1 class="text-center">购物车</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<table class="table table-bordered">
<thead>
<tr>
<th>商品id</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品数量</th>
<th><input type="checkbox" v-model="checkAll" @change="handleChange">全选/取消全选</th>
</tr>
</thead>
<tbody>
<tr v-for="good in goodList">
<td>{{good.id}}</td>
<td>{{good.name}}</td>
<td>{{good.price}}</td>
<td style="padding: 5px">
<span class="btn link btn-sm btn-default" @click="handleDown(good)">-</span>
{{good.count}}
// 下面实现商品数量的加
<span class="btn link btn-sm btn-default" @click="good.count++">+</span>
</td>
<td><input type="checkbox" v-model="checkGroup" :value="good" @change="handleCheckOne"></td>
</tr>
</tbody>
</table>
<hr>
<div v-for="good in checkGroup">
选中的商品是:{{good.name}} 数量:{{good.count}}
</div>
<br>
总价格是:{{getPrice()}}
</div>
</div>
</div>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
goodList: [
{id: 1, name: '钱包', price: 1200, count: 1},
{id: 1, name: '挎包', price: 2000, count: 2},
{id: 1, name: '书包', price: 120, count: 5},
{id: 1, name: '皮包', price: 12000, count: 3},
{id: 1, name: '面包', price: 12, count: 11},
],
checkGroup: [],
checkAll: false
},
methods: {
// 下面实现计算商品总价
getPrice() {
// 取出checkGroup中的商品数量和商品价格想成,做累加
// js中for循环
var total = 0
for (item of this.checkGroup) {
total += item.price * item.count
}
return total
},
// 下面实现选中所有商品和取消选中
handleChange() {
if (this.checkAll) {
this.checkGroup = this.goodList
} else {
this.checkGroup = []
}
},
handleCheckOne() {
// 如果 checkGroup的长度=goodList的长度 ,说明全选了,checkAll=true,否则checkAll=false
// if (this.checkGroup.length == this.goodList.length) {
// this.checkAll = true
// } else {
// this.checkAll = false
// }
// ===> 简写
this.checkAll = this.checkGroup.length == this.goodList.length
},
// 下面实现商品数量的减
handleDown(good) {
if (good.count > 1) {
good.count -= 1
} else {
alert('不能再减了,再减就没了')
}
}
}
})
</script>
</html>
v-model进阶
lazy:等待input框的数据绑定失去焦点之后再变化
number:数字开头,只保留数字,后面的字母不保留;字母开头,都保留
trim:去除首尾的空格
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="app">
<h1>v-model进阶</h1>
<input type="text" v-model.lazy="name1"> ---->{{name1}}
<br>
<input type="text" v-model.number="name2"> ---->{{name2}}
<br>
<input type="text" v-model.trim="name3"> ---->{{name3}}
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
name1: '',
name2: '',
name3: '',
},
})
</script>
</html>
Vue生命周期
1.var vm=new Vue({})
- 实例创建,数据放到实例中
- 挂载模板:el ---> div
- 改页面,改变量,都会互相影响 --- update
- 销毁实例
2.四个过程
对应八个函数
,依次进行(到某个过程就会执行某个函数)
-
beforeCreate
创建Vue实例之前调用,data,el都没有
-
created
创建实例成功后调用(此处可以发送异步请求),有data,没有el
- beforeMount
渲染DOM之前调用,有data,没有el
- mounted
渲染DOM之后调用
- beforeUpdate
重新渲染之前调用(数据更新等待操作时,控制DOM重新渲染)
- updated
重新渲染完成之后调用
- beforeDestroy
销毁之前调用
- destroyed
销毁之后调用
3.钩子函数(hook)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="app">
<h1>vue生命周期</h1>
<button @click="handleShow">点我显示组件或消失</button>
<hr>
<child v-if="show"></child>
<hr>
</div>
</body>
<script>
// 定义一个全局组件
Vue.component('child', {
template: `
<div>
<button>back</button>
{{title}}
<button @vlick="handleClick">forward</button>
</div>
`,
data() {
return {
title: '好看的首页',
t: ''
}
},
methods: {
handleClick() {
// alert('前进')
this.title = 'lqz'
}
},
beforeCreate() {
console.log('beforeCreate')
console.log(this.$data)
console.log(this.$el)
},
created() {
console.log('created')
console.log(this.$data)
console.log(this.$el)
},
beforeMount() {
console.log('beforeMount')
console.log(this.$data)
console.log(this.$el)
},
mounted() {
console.log('mounted')
console.log(this.$data)
console.log(this.$el)
},
beforeUpdate() {
console.log('beforeUpdate')
console.log(this.$data)
console.log(this.$el)
},
updated() {
console.log('updated')
console.log(this.$data)
console.log(this.$el)
},
beforeDestroy() {
console.log('beforeDestroy')
console.log(this.$data)
console.log(this.$el)
},
destroyed() {
console.log('destroyed')
console.log(this.$data)
console.log(this.$el)
},
})
var vm = new Vue({
el:'#app',
data:{
show:true
},
methods: {
handleShow() {
this.show = !this.show
}
}
})
</script>
</html>
4.与后端交互
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def index():
res = jsonify({'name': 'zhang', 'age': 20})
# 处理了跨域() 在响应头中加入 django写后端{'Access-Control-Allow-Origin': '*''}
res.headers = {'Access-Control-Allow-Origin': '*'}
return res
import json
@app.route('/films')
def films():
with open('./film.json', 'r', encoding='utf-8') as f:
data = json.load(f)
res = jsonify(data)
res.headers = {'Access-Control-Allow-Origin': '*'}
return res
if __name__ == '__main__':
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./js/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<div id="app">
<h1>jquery的Ajax与后端交互</h1>
<button @click="handleLoad1">点击加载数据</button>
<br>
<p>名字是:{{name}}</p>
<p>年龄是:{{age}}</p>
<hr>
<h1>js原生的fetch与后端交互</h1>
<button @click="handleLoad2">点击加载数据</button>
<br>
<p>名字是:{{name}}</p>
<p>年龄是:{{age}}</p>
<hr>
<h1>axios与后端交互</h1>
<button @click="handleLoad3">点击加载数据</button>
<br>
<p>名字是:{{name}}</p>
<p>年龄是:{{age}}</p>
<hr>
</div>
</body>
<script>
var vm = new Vue({
el: '#app',
data: {
name: '',
age: '',
},
methods: {
handleLoad1() {
$.ajax({
url: 'http://127.0.0.1:5000',
type: 'get',
success: data => {
console.log(typeof data)
data = JSON.parse(data)
console.log(typeof data)
this.name = data.name
this.age = data.age
}
})
},
handleLoad2() {
fetch('http://127.0.0.1:5000').then(res => res.json()).then(res => {
console.log(res)
console.log(typeof data)
this.name = data.name
this.age = data.age
})
},
handleLoad3() {
axios.get('http://127.0.0.1:5000').then(res => {
console.log(res.data)
this.name = data.name
this.age = data.age
})
},
}
})
</script>
</html>
5.电影小案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./js/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<h1>电影小案例</h1>
<ul>
<li v-for="film in filmList">
<h2>电影名:{{film.name}}</h2>
<img :src="film.poster" alt="" height="400px" width="300px">
</li>
</ul>
</div>
</body>
<script>
var vm = new Vue({
el: '#app',
data: {
filmList: []
},
created() {
axios.get('http://127.0.0.1:5000/films').then(res => {
this.filmList = res.data.data.films
})
}
})
</script>
</html>
标签:count,el,vue,console,log,data,name
From: https://www.cnblogs.com/Zhang614/p/16830193.html