样式绑定
-
1 class绑定
使用方式:v-bind:class=“expression”
expression的类型:字符串、数组、对象 -
2 style绑定
v-bind:style=“expression”
expression的类型:字符串、数组、对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<title>vue的样式绑定</title>
<style>
.a{
color: red;
}
.b{
color: aqua;
}
.c{
font-size: 36px;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li>
<h3>文本</h3>
{{msg}}
</li>
<li>
<h3>样式1</h3>
<div :class="xa">{{msg}}</div>
</li>
<li>
<h3>样式2</h3>
<div :class="xb">{{msg}}</div>
</li>
<li>
<h3>样式3</h3>
<div :class="xc">{{msg}}</div>
</li>
</ul>
</div>
</body>
<script type="text/javascript">
new Vue({
el: "#app",
data() {
return {
msg: 'hello vue',
xa:'a',
xb:'b',
xc:['a','c']
};
}
});
</script>
</html>
效果图:
事件处理器及表单的复选框、下拉框
- 阻止单击事件冒泡
<a v-on:click.stop="doThis"></a>
- 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>vue的事件处理器</title>
<style>
div{
padding: 30px;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li>
<h3>文本</h3>
{{msg}}
</li>
<li>
<h3>防止事件冒泡</h3>
<div style="height: 300px;width: 300px;background-color: orangered;" @click="a">
<div style="height: 200px;width: 200px;background-color: skyblue;" @click="b">
<div style="height: 100px;width: 100px;background-color: yellow;" @click="c">
<div style="height: 50px;width: 50px;background-color: plum;" @click.stop="d">
</div>
</div>
</div>
</div>
</li>
<li>
<h3>事件只能点击一次</h3>
{{qqmsg}}<input type="text" v-on:keyup.enter="send" v-model="msg" />
<button @click="send">发送</button>
<button @click.once="send">发送一次</button>
</li>
<li>
<h3>表单的复选框</h3>
<div v-for="item,index in hobby" >
<input type="checkbox" v-model="checkedIds" name=" hobby" :value="item.id" />{{item.name}}
</div>
{{checkedIds}}
</li>
<li>
<h3>表单的下拉框</h3>
<select name="likes" v-model="selectedId">
<option v-for="item,index in hobby" :value="item.id">{{item.name}}</option>
</select>
{{selectedId}}
</li>
</ul>
</div>
</body>
<script type="text/javascript">
new Vue({
el: "#app",
data() {
return {
msg: 'hello vue',
qqmsg: null,
hobby: [{
id: '01',
name: '篮球'
}, {
id: '02',
name: '骆驼'
}, {
id: '03',
name: '太重'
}],
checkedIds:[],
selectedId:[]
};
},
methods: {
a() {
alert('a')
},
b() {
alert('b')
},
c() {
alert('c')
},
d() {
alert('d')
},
send() {
this.qqmsg = this.msg;
this.msg = null
}
}
});
</script>
</html>
事件处理器的效果图:
表单的复选框、下拉框的效果图:
vue组件
-
组件简介
组件(Component)是Vue最强大的功能之一
组件可以扩展HTML元素,封装可重用的代码
组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面都可以抽象为一个组件树 -
全局和局部组件
全局组件:Vue.component(tagName, options),tagName为组件名,options为配置选项。
局部组件: new Vue({el:’#d1’,components:{…}}) -
注册后我们才能自定义组件,我们可以使用以下方式来调用组件:
<my-button2 ></my-button2>//名称自己定义
- props
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>
<div id="app">
<h1>组件</h1>
<ul>
<li>
<p>局部vue组件定义</p>
<my-button ></my-button>
</li>
<li>
<p>父组件传值子组件</p>
<my-button m="东东"></my-button>
</li>
<li>
<p>子组件传值父组件</p>
<my-button @three-click="dosss"></my-button>
</li>
<li>
<p>全局vue组件的注册</p>
<my-button2 ></my-button2>
</li>
</ul>
</div>
</body>
<script type="text/javascript">
Vue.component('my-button2',{
props:['m'],
template:'<button @click="doxxx" >自定义按钮,被{{m}}点击了{{n}}次</button>',
data() {
return {
n:0
}
},
methods:{
doxxx(){
this.n++;
this.$emit('three-click',this.n,'Tang','描述');
}
}
})
new Vue({
el: '#app',
data() {
return {
msg:'---vue 组件'
}
},
computed:{
xs(){
return this.n
}
},
methods:{
dosss(n,x,y){
alert(n + ' '+x +' '+ y);
}
},
components:{
'my-button':{
props:['m'],
template:'<button @click="doxxx" >自定义按钮,被{{m}}点击了{{n}}次</button>',
data() {
return {
n:0
}
},
methods:{
doxxx(){
this.n++;
this.$emit('three-click','Tang','点击了:子组件传值父组件按钮',this.n+'次');
}
}
}
}
})
</script>
</html>
效果图: