uni-app icon indicating copy to clipboard operation
uni-app copied to clipboard

小程序递归组件响应式异常,在H5页面中是正常使用的

Open Heming9 opened this issue 1 year ago • 0 comments

问题描述 小程序递归组件响应式异常,在H5页面中是正常使用的

复现步骤 [复现问题的步骤]

  1. 启动 '...'
  2. 点击 '....'
  3. 查看

[或者可以直接贴源代码]

Index.vue

<template>
	<view>
		<view class="nodes" v-for="(item, index) in value" :key="item.id">
			<view class="node">
				<view>
					{{ layer }} id({{ item.id }}): {{item.persons[0].name}} 
					<a href="javascript:void(0)" @click="onDel(item)">删除</a>..
					<a href="javascript:void(0)" @click="onAdd(item)">添加</a>..
					<a href="javascript:void(0)" @click="onAddSub(item)">添加Sub</a>
				</view>
			</view>
			<Nodes v-model="item.children" :layer="layer + 1" />
		</view>
	</view>
</template>

<script>
	import Nodes from './nodes.vue';
	export default {
		name: "Nodes",
		props: {
			value: {
				type: Array,
				default: []
			},
			layer: {
				default: 1
			}
		},
		components: {
			Nodes
		},
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {
			onDel(item) {
				this.value.splice(this.value.findIndex(i => i.id == item.id), 1)
				this.$emit('input', this.value)
				console.log('delete: ', item)
			},
			onAdd(item) {
				this.value.splice(this.value.findIndex(i => i.id == item.id), 0, {
					id: new Date().getTime(),
					persons: [
						{
							name: "new node"
						}
					],
					children: []
				})
				this.$emit('input', this.value)
				console.log('add: ')
			},
			onAddSub(item) {
				item.children.splice(0, 0, {
					id: new Date().getTime(),
					persons: [
						{
							name: "new node"
						}
					],
					children: []
				})
				this.$emit('input', this.value)
				console.log('add: ')
			}
		}
	}
</script>

nodes.vue

<template>
	<view>
		<view class="nodes" v-for="(item, index) in value" :key="item.id">
			<view class="node">
				<view style="white-space: nowrap;">
					{{ layer }} id({{ item.id }}): {{item.persons[0].name}} 
					<view style="display: inline-block;" href="javascript:void(0)" @click="onDel(item)" >删除</view>..
					<view style="display: inline-block;" href="javascript:void(0)" @click="onAdd(item)" >添加</view>..
					<view style="display: inline-block;" href="javascript:void(0)" @click="onAddSub(item)" >添加Sub</view>
				</view>
			</view>
			<Nodes v-model="item.children" :layer="layer + 1" />
		</view>
	</view>
</template>

<script>
	import Nodes from './nodes.vue';
	export default {
		name: "Nodes",
		props: {
			value: {
				type: Array,
				default: []
			},
			layer: {
				default: 1
			}
		},
		components: {
			Nodes
		},
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {
			onDel(item) {
				this.value.splice(this.value.findIndex(i => i.id == item.id), 1)
				this.$emit('input', this.value)
				console.log('delete: ', item)
			},
			onAdd(item) {
				this.value.splice(this.value.findIndex(i => i.id == item.id), 0, {
					id: new Date().getTime(),
					persons: [
						{
							name: "new node"
						}
					],
					children: []
				})
				this.$emit('input', this.value)
				console.log('add: ')
			},
			onAddSub(item) {
				item.children.splice(0, 0, {
					id: new Date().getTime(),
					persons: [
						{
							name: "new node"
						}
					],
					children: []
				})
				this.$emit('input', this.value)
				console.log('add: ')
			}
		}
	}
</script>

预期结果 H5页面: image 点击添加节点、子节点、删除时页面可以正常更新

实际结果 小程序页面 image 点击添加节点、子节点、删除时页面未正常更新,连续点击删除会因为实际数据被删除导致空指针异常

系统信息:

  • 发行平台: 微信小程序
  • 操作系统 [如 iOS 12.1.2、Android 7.0]
  • HBuilderX版本 3.4.15.20220610
  • uni-app版本 [如使用Vue-cli创建/运行项目,则提供npm run info的运行结果]
  • 设备信息 微信开发工具 Stable 1.06.2308310

补充信息 image

Heming9 avatar Feb 04 '24 04:02 Heming9