Access Pinia Getters in the Options API — Transcript

Transcript of the free Vue.js lesson Access Pinia Getters in the Options APIwatch the video lesson.

In the last lesson, we learned how to access state from a Pina store with the options API. Well, it's worth mentioning that accessing getters works exactly the same way as accessing state. Despite what you might think, there is no special map getters helper. Instead, you simply continue to use map state.

To demonstrate, over in the auth user store, let's comment out the username in the state, then set it as a getter instead. Granted, this isn't a very useful getter, but you get the idea. You'll notice that as soon as I save, in the browser you can still see the username is displayed just the same as it was before. For good measure, let's also give the page a refresh.

And yeah, the username still works. Okay, so accessing getters works exactly the same as accessing state. But what if we were to use the map writable state function instead? Over in the browser, we can actually still see the username.

While this technically works, it's a little misleading because Gitters are always going to be read-only. So if we were to try to actually update the username now, it wouldn't work. All right, so let's just put that back to map state and put username back in the state as well. And that's all there is to accessing Gitters with the options API.