最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vuejs3 - How to display object's property in child component by passing that object as a prop? - Stack Overflow

programmeradmin0浏览0评论

We use Vue 3 and the code looks as following:

Parent:

const template = ref<ShiftTemplate>({})
let dataToPass = ref<any>({})

<v-text-field-smart :data="dataToPass" />

dataToPass.value = {
    data: template.value,
    labelName: 'Name',
}

Child (v-text-field-smart):

<template>
    <v-text-field v-model="params.data">
        <template #label>{{ params.labelName }}<span class="text-red">*</span> </template>
    </v-text-field>
</template>

<script setup lang="ts">
const props = defineProps<{
    data: any
}>()

const params = toRef(props, 'data')
console.log('params =', params)
</script>

The screenshots:

If I write:

<template>
    <v-text-field v-model="params.data.name">
        <template #label>{{ params.labelName }}<span class="text-red">*</span> </template>
    </v-text-field>
</template>

I get an error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'name')
    at Proxy._sfc_render (VTextFieldSmart.vue:2:40)"

How to overcome this issue?

发布评论

评论列表(0)

  1. 暂无评论