Как получить часть данных из state в Pinia.js
Пробую pinia.js, и столкнулся с непонятным мне поведением: Имею такой стор:
import { defineStore } from 'pinia'
import axios from 'axios'
export const usePostStore = defineStore('posts', {
state: () => ({posts:null }),
getters: {
},
actions: {
async getPosts() {
await axios
.get(`https://jsonplaceholder.typicode.com/posts`)
.then(response => (this.posts = response.data));
},
},
})
И все приходит и попадает в posts. В компоненте получаю:
<script setup>
import { usePostStore } from '@/store/post.js'
import { storeToRefs } from 'pinia'
const store = usePostStore()
store.getPost()
</script>
<template>
{{ store.post.userId }}
</template>
Вопрос: почему при выводе {{ store.post}} у меня выводится без ошибок, а при попытке вывести например титл = {{ store.post.title}} - cannot read properties of null (reading 'title')
Источник: Stack Overflow на русском