一、按键修饰符
1.样式绑定
1.1 class绑定
v-bind:class="expression"
expression的类型:字符串、数组、对象
1.2 style绑定
v-bind:style="expression"
expression的类型:字符串、数组、对象
具体实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js"></script>
</head>
<style>
.f100{
font-size: 100px;
}
</style>
<body>
<!-- 定义边界 -->
<div id="app">
<h3 :style="colorRed" :class="f100">{{msg}}</h3>
</div>
</body>
<script type="text/javascript">
new Vue({
el: "#app",
data() {
return {
msg:'vue语法模板',
colorRed:'color:red',
f100:'f100',
}
}
})
</script>
</html>
页面效果:
2.冒泡事件
顾名思义,连续触发事件
具体实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js"></script>
</head>
<style>
.red{
width: 400px;
height: 400px;
background-color: red;
}
.black{
width: 300px;
height: 300px;
background-color: black;
}
.blue{
width: 200px;
height: 200px;
background-color: blue;
}
.gray{
width: 100px;
height: 100px;
background-color: gray;
}
</style>
<body>
<!-- 定义边界 -->
<div id="app">
<h2>冒泡事件</h2>
<div class="red" v-on:click="red">
<div class="black" v-on:click="black">
<div class="blue" v-on:click="blue">
<div class="gray" v-on:click.stop="gray"></div>
</div>
</div>
</div>
</div>
</body>
<script type="text/javascript">
new Vue({
el: "#app",
data() {
return {
}
},
methods:{
red(){
alert('red')
},
black(){
alert('black')
},
blue(){
alert('blue')
},
gray(){
alert('gray')
},
}
})
</script>
</html>
页面效果
因为他们是叠起来的,点击最小的正方形颜色,一定会触发四个弹框,为了阻止事件冒泡
以下代码可解决
<!-- 阻止单击事件冒泡 -->
<a v-on:click.stop="doThis"></a>
在事件后添加一个stop即可避免这种情况发生
还有其他情况:
<!-- 提交事件不再重载页面 -->
<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>
<button @click.once="submit">点击提交</button>
只能提交一次
3.按键修饰符
Vue允许为v-on在监听键盘事件时添加按键修饰符:
<input type="text" v-on:keyup.enter="submit" />
在keyup后添加以下按键名即可
全部的按键别名:
.enter
.tab
.delete (捕获 "删除" 和 "退格" 键)
.esc
.space
.up
.down
.left
.right
.ctrl
.alt
.shift
.meta
二、自定义组件
组件(Component)是Vue最强大的功能之一
组件可以扩展HTML元素,封装可重用的代码
组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面都可以抽象为一个组件树
全局组件:Vue.component(tagName, options),tagName为组件名,options为配置选项。
局部组件: new Vue({el:'#d1',components:{...}})
注意:定义组件不能用驼峰命名法
props:定义组件中的变量
具体实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js"></script>
</head>
<style>
</style>
<body>
<!-- 定义边界 -->
<div id="app">
<my-button m="小宝☞"></my-button>
</div>
</body>
<script type="text/javascript">
Vue.component("my-button",{
//m 定义的属性
props:["m"],
//template是自定义组件在页面上显示的内容
template:'<button v-on:click="addn">我被{{m}}点击了{{n}}次</button>',
data:function(){
return{
n:1,
}
},
methods:{
addn(){
this.n++;
}
}
})
new Vue({
el: "#app",
data() {
return {
}
}
})
</script>
</html>
页面展示:
点击一次n加一
三、组件通信
自定义事件
1.监听事件:$on(eventName)
2.触发事件:$emit(eventName)
具体实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js"></script>
</head>
<style>
</style>
<body>
<!-- 定义边界 -->
<div id="app">
<my-button m="小宝☞" v-on:xx-click="xxx"></my-button>
</div>
</body>
<script type="text/javascript">
Vue.component("my-button",{
//m 定义的属性
props:["m"],
//template是自定义组件在页面上显示的内容
//在button里绑定一个点击事件,当n增加到能被3整除时触发事件xx-click,在body定义的自定义按钮中绑定该事件
//由于body界面本身与new vue()发生数据交互,所以xx-click事件的方法写到new vue里,于是该组件本身与new vue形成组件通信
template:'<button v-on:click="addn">我被{{m}}点击了{{n}}次</button>',
data:function(){
return{
n:1,
}
},
methods:{
addn(){
this.n++;
if(this.n%3==0){
//可以传递任意参数
this.$emit('xx-click',this.n,'http://www.baidu.com')
}
}
}
})
new Vue({
el: "#app",
data() {
return {
}
},
methods:{
//与emit参数对应
xxx(n,path){
console.log(n)
console.log(path)
}
}
})
</script>
</html>
页面效果:
具体解释看代码注释
四、表单
代码:
<!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>
<div id="app">
<h1>标题</h1>
<ul>
<li>
<p>vue表单</p>
<label>姓名:</label><input v-model="uname" /><br />
<label>密码:</label><input v-model="upwd" type="password" /><br />
<!-- 将用户的输入值转为 Number 类型 -->
<label>年龄:</label><input v-model.number="age" /><br />
<label>性别:</label>
<input type="radio" v-model="sex" name="sex" value="1" />男
<input type="radio" v-model="sex" name="sex" value="0" />女<br />
<label>爱好:</label>
<div v-for="h in hobby">
<input type="checkbox" v-model="hobbies" v-bind:value="h.id" />{{h.name}}
</div>
<label>类别:</label>
<select v-model="type">
<option value="-1">===请选择===</option>
<option v-for="t in types" v-bind:value="t.id">{{t.name}}</option>
</select><br />
<label>备注:</label>
<textarea v-bind:value="mark"></textarea><br />
确认<input type="checkbox" v-model="flag" />
<input type="submit" v-bind:disabled="show" v-on:click="doSubmit" />
</li>
</ul>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#app',
data() {
return {
uname: null,
upwd: null,
age: 10,
sex: 1,
hobby: [{
id: 1,
name: '篮球'
}, {
id: 2,
name: '足球'
}, {
id: 3,
name: '象棋'
}],
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+10,
sex: this.sex,
hobbies:this.hobbies,
type: this.type,
mark: this.mark,
}
console.log(obj);
}
}
})
</script>
</html>
页面效果:
当确认框勾选时方可提交,提交键默认为禁用