Comments (6)
我理解错误了,还是单向的。通过watch可以实现双向绑定,下面是实现的代码,供大家参考。
<template>
<view>
<!--开关组件-->
<switch-button :result="result" @toFather="toFather"></switch-button>
<!--外部控制-->
<input type="button" value="change" @click="change">
<text>{{ result }}</text>
</view>
</template>
<script>
import switchButton from '../../components/switch-button.vue'
export default {
data: {
result: true //开关状态数据
},
methods: {
change() {
this.result = !this.result;
},
toFather(val) {
this.result = val;
}
},
components: {
switchButton,
},
}
</script>
<style>
view {
display: block;
}
</style>
//开关组件代码
<template>
<view @click='change'>{{result?'开':'关'}}</view>
</template>
<script>
export default {
props: {
result: {
type: Boolean,
default: false
},
},
methods: {
change() {
this.result = !this.result;
}
},
data() {
return {
myResult: this.result,
}
},
watch: {
result(val) {
this.myResult = val
},
myResult(val) {
this.$emit('toFather', val)
}
}
}
</script>
from uni-app.
//开关组件代码
<template>
<view @click='change'>{{result?'开':'关'}}</view>
</template>
<script>
export default {
props: {
result: {
type: Boolean,
default: false
},
},
methods: {
change() {
this.result = !this.result;
}
},
// data() {
// return {
// myResult: this.result,
// }
// },
// watch: {
// result(val) {
// this.myResult = val
// },
// myResult(val) {
// this.$emit('toFather', val)
// }
// }
}
</script>
这里好像不使用watch实现双向绑定也可以?是不是在新版的vue中prop又改为了双向绑定了。
from uni-app.
<template>
<view>
<!--开关组件-->
<switch-button :result="result"></switch-button>
<!--外部控制-->
<input type="button" value="change" @click="change">
<text>{{ result }}</text>
</view>
</template>
<script>
import switchButton from '../../components/switch-button.vue'
export default {
data: {
result: true //开关状态数据
},
methods: {
change() {
this.result = !this.result;
}
},
components: {
switchButton,
},
}
</script>
<style>
view {
display: block;
}
</style>
from uni-app.
你是怎么测试的。。修改子组件的 result,发现父组件的 result 改变了?我这里测试没有发现这个问题
from uni-app.
已经解决双向绑定的问题
from uni-app.
请问双向绑定不能用v-model吗
from uni-app.
Related Issues (20)
- 微信小程序报错 TypeError: t.$callHook is not a function
- @dcloudio/uni-app@vue3 自定义组件使用插槽接收参数时会生成一个空view HOT 1
- 移动端video如何播放本地m3u8文件
- 可视化创建的项目,怎么总报错啊
- uni.uploadFile 在华为手机鸿蒙4.X系统上,上传到开发服务器上,httpStatus=0,导致无法上传 HOT 1
- 抖音小程序添加了【配置行业 SDK 的权限】,需要对编译后的文件手动增加配置项
- BackgroundAudioManager 监听原生 onPrev 事件不触发
- 新增对vite5的支持 HOT 2
- 新增对vue3.4的支持 HOT 2
- 不是bug HOT 2
- Uni-App 什么时候支持华为“鸿蒙应用”和“元服务”呢?
- 抖音 动态循环里的 if 里的 ref 无法获取到虽然按照 https://github.com/dcloudio/uni-app/issues/4554 这个方法解决了;但是带来了新的问题:自定义组件变成页面了。
- input type=text Error HOT 1
- 小程序误报 Property "xx" was accessed during render but is not defined on instance HOT 4
- uniapp vue3 最新版本sdk打包的小红书小程序为何每次返回会自动刷新页面?
- uni-app ios真机debug 控制台报错
- uni-app android SDK下载困难
- 编译到小程序,login-page.vue文件不更新 HOT 2
- chooseImage 拍照返回的file是空,只在Oppo reno 2Z手机上出现
- uniapp ios静态资源在真机上显示不出来 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uni-app.