question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow getters to have a setter defined

See original GitHub issue

Like computed properties

getters: {
	stuff: {
		get() {}
		set() {}
	},
	otherStuff() {
		// only getter
	}
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
dmolesUCcommented, Jul 6, 2022

@jd-solanki I don’t know if this is the preferred syntax now, but what I’ve been doing is this:

import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

// note 'options' argument is an arrow function, not an object literal
export const useStore = defineStore('myStore', () => { 

  // state
  const foo = ref(null)

  // ordinary (if contrived) Vue computed property w/getter and setter
  const isBar = computed(() => {
    get() {
       const fooVal = foo.value
       return (!!fooVal) && (!!fooVal.bar)
    },
    set(v) {
       const fooVal = foo.value || {}
       fooVal.bar = !!v
       foo.value = fooVal
    }
  })

  return { foo, isBar }
})

I assume this is the “new function syntax” mentioned above, but I don’t really know – it doesn’t seem to be documented anywhere in the Guide. It’s less structured than the state/getters/actions syntax but much more flexible.

5reactions
Fanna1119commented, Jul 5, 2021

this would be cool. At the moment i just use a computed getter/setter, which works as expected.

//vue component

        const themes = useThemes();
        const activeTheme = computed({
            get: () => themes.getTheme,
            set: val => {
                themes.setTheme(val)
            }
        })

//store

export const useThemes = defineStore({
    id: 'themeSelect',
    state: () => (
        {
            ActiveTheme: useStorage("active-theme", "emerald"),
            availableThemes: ['emerald', 'dark', 'forest', 'synthwave']
        }
    ),
    getters: {
        getTheme() {
            return computed(() => this.ActiveTheme)
        }
    },
    actions: {
        setTheme(val) {
            this.ActiveTheme = val;
        }
    }

})

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getters and Setters in Java Explained - freeCodeCamp
Getters and setters allow control over the values. You may validate the given value in the setter before actually setting the value.
Read more >
Property getters and setters - The Modern JavaScript Tutorial
Accessor properties are represented by “getter” and “setter” methods. In an object literal they are denoted by get and set : let obj...
Read more >
@Getter and @Setter - Project Lombok
A default getter simply returns the field, and is named getFoo if the field is called foo (or isFoo if the field's type...
Read more >
Allow getters to have a setter defined · Issue #447 · vuejs/pinia
At the moment i just use a computed getter/setter, which works as expected. //vue component. const themes = useThemes(); const ...
Read more >
Significance of Getters and Setters in Java - Baeldung
Getters and Setters play an important role in retrieving and updating the value of a variable outside the encapsulating class.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found