Vue模板语法(下)

样式绑定

  • 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>


效果图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值