CodexBloom - Programming Q&A Platform

Vue 3: Computed Property Not Reacting to Nested Object Changes in v-model Binding

👀 Views: 47 💬 Answers: 1 📅 Created: 2025-06-14
vue.js vue3 computed-properties JavaScript

I'm prototyping a solution and I'm trying to configure I'm migrating some code and I’m working with Vue 3 and I’ve run into an scenario where a computed property isn't updating as expected when I modify a nested object that is bound using `v-model`... I have a component that allows users to edit a user's profile, and the profile data is structured as a nested object. Here’s my current setup: ```javascript <template> <div> <input v-model="userProfile.name" placeholder="Name" /> <input v-model="userProfile.address.street" placeholder="Street" /> <p>Full Address: {{ fullAddress }}</p> </div> </template> <script setup> import { ref, computed } from 'vue'; const userProfile = ref({ name: '', address: { street: '', city: '' } }); const fullAddress = computed(() => { return `${userProfile.value.address.street}, ${userProfile.value.address.city}`; }); </script> ``` I expected that when `userProfile.address.street` or `userProfile.address.city` changes, the `fullAddress` computed property would automatically update. However, I notice that it does not react when I directly mutate the nested properties like this: ```javascript userProfile.value.address.street = '123 Main St'; userProfile.value.address.city = 'New York'; ``` When I inspect `fullAddress`, it still shows the initial empty values rather than the updated address. I’ve tried using Vue's `set` method, but I can’t seem to find a way to make it work with nested properties. What am I doing wrong here? Is there a recommended approach for ensuring computed properties react properly to changes in nested objects? I’m using Vue 3.2.30, and my setup is just a standard Vue CLI project. Any help would be appreciated! This is my first time working with Javascript 3.10. Thanks in advance! I'm working with Javascript in a Docker container on Windows 11. Thanks for your help in advance! I'm working on a application that needs to handle this. Any ideas what could be causing this?