0

I'm trying to include get() and set() functions inside the computed method using Composition API. However, this is possible using an option API like this.

export default {
  name: "Home",
  computed: {
    colorCode: {
        get(){

        },
        set(){
            
        }
    }
  },
};

Is there any way to do this same task with Composition API? Really appreciate if somebody can help me in this situation, I was trying like this,

setup() {
    const colorCodeGet = computed(() => {
      return $store.state.colorCode;
    });

    const colorCodeSet = computed(() => {});

    return { colorCode };
  },
1

1 Answer 1

1

as mentioned here, You can do it like this:

<script lang="ts">
import { defineComponent, computed, WritableComputedRef } from 'vue';

interface IBook {
  name: string;
  author: string;
}

class Book implements IBook {
  name: string;
  author: string;

  constructor(name, author) {
    this.name = name;
    this.author = author;
  }
}

export default defineComponent({
  setup() {
    const book: WritableComputedRef<IBook> = computed({
      get(): IBook {
        return bookTakenFromSomePlace;
      },
      set(newBook: IBook): void {
        bookOldValue = new Book(newBook.name, newBook.author);
      },
    });

    return {
      book,
    };
  }
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.