再次认识 v-model

v-model —>

:value=“variable” +

@input=“variable=$event.target.value”

---------------------------分割线-----------------------------------------------

在父组件使用v-model A.监听 input B.绑定 variable

<input ref=“input” :value=“value” @input="$emit(‘input’, $event.target.value)">

子组件 value 是父组件的value

子组件 props: [‘value’],

子组件 @input="$emit(‘input’, $event.target.value)"

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
		<script src="https://cdn.bootcss.com/vue/2.3.0/vue.js"></script>
	</head>
	<body>
		<div id="demo">
			<span>{{price}}</span>
			<br>
			<!-- <currency-input v-model="price"></currentcy-input> -->
			<currency-input :value="price" @input="price = arguments[0]"></currentcy-input>
		</div>
		<template id="cin">
			<span>
				<input ref="input" :value="value" @input="$emit('input', $event.target.value)">
			</span>
		</template>
		<!--为什么这里把 'input' 作为触发事件的事件名?`input` 在哪定义的?-->
	</body>
	<script>
		Vue.component('currency-input', {
			template: "#cin",
			props: ['value'],
		})
		// 为什么这里要用 value 属性,value在哪里定义的?貌似没找到啊?
		var demo = new Vue({
			el: '#demo',
			data: {
				price: 100,
			},
			watch:{
				price(val){
				console.log("watch---",val)	
				}
			}
		})
	</script>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值