nuxt-community / composition-api

Composition API hooks for Nuxt 2.
https://composition-api.nuxtjs.org
MIT License
709 stars 100 forks source link

help: Computed property based on an object prop #750

Closed rebinnaf closed 1 year ago

rebinnaf commented 1 year ago

📚 What are you trying to do? Please describe. I am not sure how to have an object prop without specific properties and then have a computed property based on it. I added a reproduction of my problem, I have a component that gets some data as prop and want to format it and then show it. But, although in the options api it is reactive, It is not reactive in composition api.

Reproduction

🔍 What have you tried? If I define const field = ref({name:''}); it works but in my case I don't know what would be the response of the server

ℹī¸ Additional context

rchl commented 1 year ago

I suppose this is a composition API question and not really specific to this module. You can probably do something like this in your component:

  setup(props) {
    const formattedName = computed(() => {
      if (props.field.name) {
        return `name:${props.field.name}`;
      }
    });
    return { formattedName };
  }