一、样式绑定
1.1 class绑定
使用方式:v-bind:class="expression"
expression的类型:字符串、数组、对象
1.2 style绑定
v-bind:style="expression"
expression的类型:字符串、数组、对象
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js"></script>
<style>
.f200{
font-size:100px;
}
</style>
</head>
<body>
<!-- 定义边界 -->
<div id="app">
<h3 :class="f200" v-bind:style="colorred">{{msg}}</h3>
</div>
</body>
<script type="text/javascript">
<!-- 绑定边界 -->
new Vue({
el:'#app',
data(){
return {msg:'三丰,我是你大爷',
colorred:'color:red',
f200:'f200'
};
}
})
</script>
</html>
效果:
二、事件
2.1 事件修饰符
<!-- 阻止单击事件冒泡 -->
<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>
2.2 按键修饰符
Vue允许为v-on在监听键盘事件时添加按键修饰符:
<!-- 只有在 keyCode 是 13 时调用 vm.submit() -->
<input v-on:keyup.13="submit">
Vue为最常用的按键提供了别名
<!-- 同上 -->
<input v-on:keyup.enter="submit">
全部的按键别名:
.enter
.tab
.delete (捕获 "删除" 和 "退格" 键)
.esc
.space
.up
.down
.left
.right
.ctrl
.alt
.shift
.meta
代码:<!-- 阻止单击事件冒泡 --><!-- click 事件只能点击一次 --> .enter只演示了这三种 其他都是一样的写法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js"></script>
<style>
.red{
width: 400px;
height: 400px;
background-color: red;
}
.orange{
width: 300px;
height: 300px;
background-color: orange;
}
.blue{
width: 200px;
height: 200px;
background-color: blue;
}
.black{
width: 100px;
height: 100px;
background-color: black;
}
</style>
</head>
<body>
<!-- 定义边界 -->
<div id="app">
<p>冒泡事件</p>
<div class="red" @click="red">
<div class="orange" @click="orange">
<div class="blue" @click="blue">
<div class="black" @click.stop="black"></div>
</div>
</div>
</div>
<p>提交事件</p>
<button @click.once="dothis">提交</button>
<p>案件修饰符</p>
<input v-on:keyup.enter="dothis">
</div>
</body>
<script type="text/javascript">
<!-- 绑定边界 -->
new Vue({
el:'#app',
data(){
return {
f200:'f200'
};
},
methods:{
red(){
alert("red");
},
orange(){
alert("orange");
},
blue(){
alert("blue");
},
black(){
alert("black");
},
dothis(){
alert("已完成,提交答案")
}
}
})
</script>
</html>
效果:
三、表单
基本上都是学过的 需要注意的点是
确认<input type="checkbox" v-model="flag" />
<input type="submit" v-bind:disabled="show" v-on:click="doSubmit" />
flag: false
}
},
computed: {
show: function() {
return !this.flag;
}
当复选框被勾选的时候flag等于true,而show等于false 就不隐藏
当复选框没被勾选的时候flag等于false,而show等于true就隐藏
总代码:
<!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>
效果:
四、组件
注意:
变量定义不能使用中划线,参照案例样式绑定命名错误
自定义组件命名不能使用驼峰
组件(Component)是Vue最强大的功能之一
组件可以扩展HTML元素,封装可重用的代码
组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面都可以抽象为一个组件树
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js"></script>
<style>
.f200{
font-size:100px;
}
</style>
</head>
<body>
<!-- 定义边界 -->
<div id="app">
<!-- 定义组件不能用驼峰命名 -->
<my-button m="三丰">我的组件</my-button>
<my-button m="张三">我的组件</my-button>
</div>
</body>
<script type="text/javascript">
Vue.component('my-button',{
// props是定义组件中的变量的
props:["m"],
// template代表了自定义组件在页面上显示的内容
template:'<button v-on:click="incrn">我被{{m}}点击了{{n}}次</button>',
data:function(){
return{
n:1
}
},
methods:{
incrn(){
this.n++;
}
}
});
<!-- 绑定边界 -->
new Vue({
el:'#app',
data(){
return {
f200:'f200'
};
}
})
</script>
</html>
效果:
五、组件通信
监听事件:$on(eventName)
触发事件:$emit(eventName)
注1:Vue自定义事件是为组件间通信设计
vue中父组件通过prop传递数据给子组件,而想要将子组件的数据传递给父组件,则可以通过自定义事件的绑定
父Vue实例->Vue实例,通过prop传递数据
子Vue实例->父Vue实例,通过事件传递数据
注2:事件名
不同于组件和prop,事件名不存在任何自动化的大小写转换。而是触发的事件名需要完全匹配监听这个事件所用的名称
建议使用“短横线分隔命名”,例如:three-click
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
</head>
<body>
<div id="app">
<ul>
<li>
<h3>{{ts}}</h3>
<p>vue组件</p>
<!-- 使用自定义组件my-button的时候进行传值(相当于jsp标签往助手类中传值的概念) -->
<my-button m="小李飞刀" v-on:three-click="xxx"></my-button>
</li>
</ul>
</div>
</body>
<script>
// 定义全局组件的方式
Vue.component('my-button', {
props: ['m'],
template: '<button v-on:click="doClickMyButton">我是一个自定义组件,被{{m}}点了{{n}}次</button>',
data: function() {
return {
n: 0
};
},
methods: {
doClickMyButton: function() {
console.log('doClickMyButton方法被调用');
this.n++;
if (this.n % 3 == 0) {
// 触发自定义组件定义的事件,这里可以传递容易任意个参数
// 但是触发的事件绑定的函数要与参数个数数一致
this.$emit('three-click', this.n, 'http://www.javaxl.com', '小李飞刀');
}
}
}
})
var vm = new Vue({
el: "#app",
data: {
ts: new Date().getTime()
},
methods: {
xxx:function(a,b,c){
console.log("自定义事件绑定的函数被执行。。。")
console.log(a);
console.log(b);
console.log(c);
}
}
});
</script>
</html>
效果: