JavaScript/vue.js/computed:修订间差异
来自康健生活
< JavaScript | vue.js
创建页面,内容为“== 标题文本 ==” |
|||
| 第1行: | 第1行: | ||
== 标题文本 == | == 标题文本 == | ||
== 参数透传 == | |||
三层嵌套组件,祖父组件向孙子组件传递 v-model 时可以借助 computed 进行参数透传。 | |||
祖父组件 | |||
<syntaxhighlight lang="html5"> | |||
<template> | |||
<ApiModal v-model="visible" /> | |||
</template> | |||
<script setup> | |||
import { ref } from 'vue' | |||
const visible = ref(true) | |||
</script> | |||
</syntaxhighlight> | |||
父组件进行参数透传,或者说某种程度上的参数代理。 | |||
<syntaxhighlight lang="html5"> | |||
<template> | |||
<Modal v-model="visible" /> | |||
</template> | |||
<script setup> | |||
import { computed, defineEmits, defineProps } from 'vue' | |||
const emit = defineEmits(['update:modelValue']) | |||
const props = defineProps({ | |||
modelValue: Boolean, | |||
}) | |||
const visible = computed({ | |||
get: () => props.modelValue, | |||
set: (value) => emit('update:modelValue', value), | |||
}) | |||
</script> | |||
</syntaxhighlight> | |||
孙子组件 | |||
<syntaxhighlight lang="html5"> | |||
<template></template> | |||
<script setup> | |||
import { defineProps } from 'vue' | |||
const props = defineProps({ | |||
modelValue: Boolean, | |||
}) | |||
</script> | |||
</syntaxhighlight> | |||
2025年4月3日 (四) 07:06的最新版本
标题文本
参数透传
三层嵌套组件,祖父组件向孙子组件传递 v-model 时可以借助 computed 进行参数透传。
祖父组件
<template>
<ApiModal v-model="visible" />
</template>
<script setup>
import { ref } from 'vue'
const visible = ref(true)
</script>
父组件进行参数透传,或者说某种程度上的参数代理。
<template>
<Modal v-model="visible" />
</template>
<script setup>
import { computed, defineEmits, defineProps } from 'vue'
const emit = defineEmits(['update:modelValue'])
const props = defineProps({
modelValue: Boolean,
})
const visible = computed({
get: () => props.modelValue,
set: (value) => emit('update:modelValue', value),
})
</script>
孙子组件
<template></template>
<script setup>
import { defineProps } from 'vue'
const props = defineProps({
modelValue: Boolean,
})
</script>
No categories