uni-app组件和组件之间的双向绑定

父组件中的子组件使用v-model,在子组件的props中也可以接收,实现组件和组件之间的双向绑定

父组件中引入了子组件narbar, 并绑定了value 

<navbar :isSearch="true" @input="inputChange" v-model="value"></navbar>

子组件,使用props接收并进行监听, 然后使用this.$emit将变化的值又发送给父组件

export default {
		name: "navbar",
		props:{
			value:{
				type:[String,Number],
				default:''
			},
			isSearch:{
				type:Boolean,
				default:false
			}
		},
		data() {
			return {
				statusBarHeight: 20,
				navbarHeight: 45,
				windowWidth: 375,
				val:''
			};
		},
		watch:{
			value(newVal){
				this.val = newVal
			}
		},
		// 组件加载
		created() {
			// 同步获取手机系统信息
			let info = uni.getSystemInfoSync()
			// 动态设置状态栏高度
			this.statusBarHeight = info.statusBarHeight
			// 设置搜索框的宽度
			this.windowWidth = info.windowWidth

			/* 条件编译 不等于这三个平台 , 只会在微信小程序,QQ,百度小程序运行*/
			// #ifndef H5 || MP-ALIPAY || APP-PLUS
			/* 动态设置导航栏的高度 */
			// 获取胶囊位置
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			// 导航栏高度 = (胶囊底部 - 状态栏高度) + (胶囊顶部 - 状态栏高度)
			this.navbarHeight = (menuButtonInfo.bottom - this.statusBarHeight) + (menuButtonInfo.top - this
				.statusBarHeight)
			// 设置搜索框的宽度
			this.windowWidth = menuButtonInfo.left
			// #endif
		},
		methods:{
			open(){
				if(this.isSearch) return
				uni.navigateTo({
					url: '/pages/home-search/home-search'
				});
			},
			// 监听输入框的变化
			inputChange(e){
				let {value} = e.detail
				this.$emit('input',value)
			}
		}
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值