Vue模板语法(下)
- 样式绑定
1.1 class绑定
使用方式:v-bind:class=“expression”
expression的类型:字符串、数组、对象
1.2 style绑定
v-bind:style=“expression”
expression的类型:字符串、数组、对象
<!-- 阻止单击事件冒泡 -->
<a v-on:click.stop="doThis"></a>
<!-- 提交事件不再重载页面 -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- 修饰符可以串联 -->
<a v-on:click.stop.prevent="doThat"></a>
<!-- 只有修饰符 -->
<form v-on:submit.prevent></form>
<!-- 添加事件侦听器时使用事件捕获模式 -->
<div v-on:click.capture="doThis">...</div>
<!-- 只当事件在该元素本身(而不是子元素)触发时触发回调 -->
<div v-on:click.self="doThat">...</div>
<!-- click 事件只能点击一次 -->
<a v-on:click.once="doThis"></a>
样式绑定
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<title>样式绑定</title>
</head>
<style>
.a {
background-color: greenyellow;
}
.b {
background-color: aqua;
}
.c {
font-size: 60px;
}
div {
padding: 25px;
}
</style>
<body>
<!-- vue所管理的边界 -->
<div id="app">
<ul>
<li>
<h2>样式绑定</h2>
<span class="a">你好难</span>
<hr />
<span :class="bclz">你好大胆</span>
</li>
<li>
<h2>事件冒泡</h2>
<h3>click.stop 阻止事件</h3>
<div style="background-color: aliceblue;height: 300px;width: 300px;" @click="d">
<div style="background-color:aqua ;height: 200px;width: 200px;" @click="c">
<div style="background-color: cadetblue;height: 100px;width: 100px;" @click="b">
<div style="background-color: blue;height: 50px;width: 50px;" @click.stop="a">
</div>
</div>
</div>
</div>
</li>
<li>
<h2>模拟QQ消息发送(某些按钮只能点击一次)</h2>
发送消息:<input v-model="msg" v-on:keyup.enter="send"/>
接受消息:{{info}}
<br />
<button @click="send">点击发送</button>
<button @click.once="send">只能发送一次</button>
</li>
</ul>
</div>
<script>
//通过vue实例渲染vue所管理的边界
new Vue({
el: '#app',
data() {
return {
aclz: 'b',
bclz: ['b', 'c'],
msg:'',
info:''
}
},
methods: {
a() {
alert("事件A被触发");
},
b() {
alert("事件B被触发");
},
c() {
alert("事件C被触发");
},
d() {
alert("事件D被触发");
},
send(){
this.info = this.msg;
this.msg = '';
}
}
})
</script>
</body>
</html>
一些按键的别名
.enter
.tab
.delete (捕获 "删除" 和 "退格" 键)
.esc
.space
.up
.down
.left
.right
.ctrl
.alt
.shift
.meta
表单
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<title>表单</title>
</head>
<body>
<!--vue管理边界-->
<div id="app">
<h1>这是表单</h1>
<ul>
<li>
<h2>vue表单</h2>
<label>姓名:</label><input v-model="uname" /><br />
<label>密码:</label><input v-model="upwd" type="password" /><br />
<!-- v-model.number 将用户的输入值转为 Number 类型 -->
<label>年龄:</label><input v-model.number="age" /><br />
<label>性别:</label>
<!-- 性别单选 以 1 0 表示男女 -->
<input type="radio" v-model="sex" name="sex" value="1" />男
<input type="radio" v-model="sex" name="sex" value="0" />女<br />
<label>爱好:</label>
<!-- v-for -->
<div v-for="item in hobby">
<input type="checkbox" v-model="hobbies" v-bind:value="item.id" />{{item.name}}
</div>
<label>类别:</label>
<select v-model="type" >
<option value="-1" >===请选择===</option>
<option v-for="item in types" v-bind:value="item.id">{{item.name}}</option>
</select><br />
<label>备注:</label>
<textarea v-model="mark"></textarea><br />
<!--需要注意的是,textarea 的默认值-->
<!-- 以flag的值来控制 show 选中时,flag=true show=false disabled=false-->
确认<input type="checkbox" v-model="flag" />
<input type="submit" v-bind:disabled="show" v-on:click="doSubmit" />
</li>
</ul>
</div>
</body>
<script >
// vue实例
new Vue({
el: '#app',
data() {
return {
uname: null,
upwd: null,
age: 20,//默认值
sex: 1,
hobby: [{//爱好
id: 1,
name: '唱'
}, {
id: 2,
name: '跳'
}, {
id: 3,
name: 'rap'
}],
hobbies: [],//存储选中的爱好
types: [{
id: 1,
name: 'A'
}, {
id: 2,
name: 'B'
}, {
id: 3,
name: 'C'
}],
type: null,//存储类别
mark:'学生备注',
flag: false
}
},
computed: {
show: function() {
return !this.flag;
}
},
methods: {
doSubmit: function() {
console.log('doSubmit')
var obj = {
uname: this.uname,
upwd: this.upwd,
age:this.age,
sex: this.sex,
hobbies:this.hobbies,
type: this.type,
mark: this.mark,
}
console.log(obj);
}
}
})
</script>
</html>
vue组件
5.1 组件简介
组件(Component)是Vue最强大的功能之一
组件可以扩展HTML元素,封装可重用的代码
组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面都可以抽象为一个组件树
props是父组件用来传递数据的一个自定义属性。
父组件的数据需要通过props把数据传给子组件,子组件需要显式地用props选项声明 "prop"
组件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<title>组件</title>
</head>
<body>
<!-- vue所管理的边界 -->
<div id="app">
<ul>
<li>
<h2>局部组件的注册</h2>
<my-button></my-button>
</li>
<li>
<h2>父子组件的传参(父传子)</h2>
<my-button m="zs"></my-button>
<my-button m="ls"></my-button>
</li>
<li>
<h2>父子组件的传参(子传父)</h2>
<h3>定义自定义事件去完成</h3>
<my-button v-on:three-click="getData"></my-button><br />
接受自定义组件中内部的变量值:{{innerNum}}<br />
作者:{{ar}}
<hr />
<my-button v-on:three-click="getData"></my-button><br />
接受自定义组件中内部的变量值:{{innerNum}}<br />
作者:{{ar}}
</li>
<li>
<h2>全局组件的注册</h2>
<my-button2></my-button2>
</li>
</ul>
</div>
<script>
Vue.component('my-button2',{
props:["m"],
template:'<button v-on:click="add">被{{m}}点击了{{n}}次</button>',
data(){
return {
n:1,
auther:'DZY'
}
},
methods:{
add(){
this.n = this.n+1;
//自定义一个三击事件
if(this.n%3==0){
this.$emit('three-click',this.n,this.auther);
}
}
}
})
//通过vue实例渲染vue所管理的边界
new Vue({
el: '#app',
data() {
return {
innerNum:1,
ar:''
}
},methods:{
getData(v,a){
this.innerNum=v;
this.ar = a;
}
},
components:{
'my-button':{
props:["m"],
template:'<button v-on:click="add">被{{m}}点击了{{n}}次</button>',
data(){
return {
n:1,
auther:'DZY'
}
},
methods:{
add(){
this.n = this.n+1;
//自定义一个三击事件
if(this.n%3==0){
this.$emit('three-click',this.n,this.auther);
}
}
}
}
}
})
</script>
</body>
</html>