l***@qq.com
l***@qq.com
  • 发布:2020-04-15 22:37
  • 更新:2023-08-28 14:42
  • 阅读:1527

Swtich无法进行双向绑定

分类:uni-app

请看代码

<template>  
	<view class="container">  
		<view class="intro">本项目已包含uni ui组件,无需import和注册,可直接使用。在代码区键入字母u,即可通过代码助手列出所有可用组件。光标置于组件名称处按F1,即可查看组件文档。</view>  
		<text class="intro">详见:</text>  
		<uni-link :href="//ask.dcloud.net.cn/href" :text="href"></uni-link>  
		<switch type="switch" @change="Onchange" :checked="swTest"  v-model="swTest"></switch>  
	</view>  
</template>  
  
<script>  
	  
	export default {  
		data() {  
			return {  
				href: 'https://uniapp.dcloud.io/component/README?id=uniui',  
				swTest:false  
			}  
		},  
		methods: {  
			Onchange:function(e){  
				console.log(this.swTest)  
				var that = this  
				setTimeout(function()  
				{  
					console.log(" test==> "   that.swTest)  
					that.swTest=true  
				},2000)  
			}  
		}	  
  
	}  
</script>  
  
<style>  
	.container {  
		padding: 20px;  
		font-size: 14px;  
		line-height: 24px;  
	}  
</style>  
  

测试结果,发现变量'swTest',只有在开始的时候能够初始化Switch的状态
通过点击后,定时器查看'swTest'的值一直为true,说明双向绑定是不行的
另外在浏览器的Console下修改'swTest'的值为true或者false都无法改变switch的状态

2020-04-15 22:37 负责人:无 分享
已邀请:
pandelix

pandelix

同问

anlogo

anlogo

也遇到这个问题, 楼主最后有解决吗?

2***@qq.com

2***@qq.com

看官方文档,没有v-model参数啊,是不是你checked和v-model一起用了的原因

要回复问题请先登录注册