GithubHelp home page GithubHelp logo

Comments (6)

zengqingsong avatar zengqingsong commented on April 28, 2024 1

我理解错误了,还是单向的。通过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.

zengqingsong avatar zengqingsong commented on April 28, 2024
//开关组件代码
<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.

zengqingsong avatar zengqingsong commented on April 28, 2024
<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.

fxy060608 avatar fxy060608 commented on April 28, 2024

你是怎么测试的。。修改子组件的 result,发现父组件的 result 改变了?我这里测试没有发现这个问题

from uni-app.

zengqingsong avatar zengqingsong commented on April 28, 2024

已经解决双向绑定的问题

from uni-app.

bacy avatar bacy commented on April 28, 2024

请问双向绑定不能用v-model吗

from uni-app.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.