状态内部的数组在使用 Pinia 时始终为未定义。

huangapple go评论66阅读模式
英文:

the array inside of the state is always undefind when using pinia

问题

我在我的Vue 3项目中使用Pinia,并因某种原因尝试将一些临时数据存储在Pinia中。以下是我的存储文件:

// template.ts
export const useTemplateStore = defineStore('template', {
  state: () => {
    return {
      templates: [] as Template[],
      temporary: {
        id: generateUUID(),
        itemList: []
      } as Template
    }
  },
  actions: {
    reset() {
      this.temporary = {
        id: generateUUID(),
        itemList: []
      }
    },
    addItem(item: TemplateItem) {
      this.temporary.itemList.push(item);
    }
  }
})

但当我尝试将项目添加到temporary.itemList时,它失败了:

<script setup lang="ts">
// ...
const useTemplate = useTemplateStore();
// 两者都失败了
useTemplate.addItem({ ... });
useTemplate.temporary.itemList.push({ ... });
</script>

当我检查控制台时,它显示:

直接推送

使用addItem方法

我将itemList记录出来(console.log(useTemplate.temporary.itemList)),结果是undefined

问题出在哪里?这让我很困扰,如果有人能帮助我,我将不胜感激。

顺便说一下,我的项目是使用TypeScript编写的。

英文:

I am using pinia in my vue3 projects and for some reason I am trying to store some temporary data in pinia, here is my store file:

// template.ts
export const useTemplateStore = defineStore(&#39;template&#39;, {
  state: () =&gt; {
    return {
      templates: [] as Template[],
      temporary: {
        id: generateUUID(),
        itemList: []
      } as Template
    }
  },
  actions: {
    reset() {
      this.temporary = {
        id: generateUUID(),
        itemList: []
      }
    },
    addItem(item: TemplateItem) {
      this.temporary.itemList.push(item);
    }
  }
})

But when I tried to push item into temporary.itemList in my vue file, it failed:

&lt;script setup lang=&quot;ts&quot;&gt;
// ...
const useTemplate = useTemplateStore();
// both of them failed
useTemplate.addItem({ ... });
useTemplate.temporary.itemList.push({ ... });
&lt;/script&gt;

when I checked the console, it showed:
directly push
using addItem method

I log the itemList out(console.log(useTemplate.temporary.itemList)), and the result is undefined.

where is the problem? it bothers me very much,so if anyone can help me out I'd appreciate it.

By the way, my projects is coding with typescript.

答案1

得分: 0

这可能是由TypeScript引起的问题。

JavaScript版本运行良好。

const { createApp } = Vue
const { createPinia, defineStore } = Pinia

const useTemplateStore = defineStore('template', {
  state: () => {
    return {
      templates: [],
      temporary: {
        itemList: []
      } 
    }
  },
  actions: {
    reset() {
      this.temporary = {
        itemList: []
      }
    },
    addItem(item) {
      this.temporary.itemList.push(item);
    }
  }
})

const App = {
  setup() {
    const useTemplate = useTemplateStore();
    // both of them failed
    useTemplate.addItem({ item: 1 });
    const addItem2 = () => { 
      useTemplate.temporary.itemList.push({ item: useTemplate.temporary.itemList.length + 1}) 
    }
    return {
      useTemplate,
      addItem2
    }
  }
}

const app = createApp(App)
const pinia = createPinia()
app.use(pinia)
app.mount('#app')
#app { line-height: 1.75; }
[v-cloak] { display: none; }
<div id="app" v-cloak>
  useTemplate.temporary.itemList: {{ useTemplate.temporary.itemList }}<br/>
  <button @click="addItem2()">Add Item</button>&nbsp;
  <button @click="useTemplate.reset()">Reset</button>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="https://unpkg.com/vue-demi@0.13.11/lib/index.iife.js"></script>
<script src="https://unpkg.com/pinia@2.0.30/dist/pinia.iife.js"></script>
英文:

It could be something caused by TypeScript.

The JavaScript version works well.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const { createApp } = Vue
const { createPinia, defineStore } = Pinia

const useTemplateStore = defineStore(&#39;template&#39;, {
  state: () =&gt; {
    return {
      templates: [],
      temporary: {
        itemList: []
      } 
    }
  },
  actions: {
    reset() {
      this.temporary = {
        itemList: []
      }
    },
    addItem(item) {
      this.temporary.itemList.push(item);
    }
  }
})

const App = {
  setup() {
    const useTemplate = useTemplateStore();
    // both of them failed
    useTemplate.addItem({ item: 1 });
    const addItem2 = () =&gt; { 
      useTemplate.temporary.itemList.push({ item: useTemplate.temporary.itemList.length + 1}) 
    }
    return {
      useTemplate,
      addItem2
    }
  }
}

const app = createApp(App)
const pinia = createPinia()
app.use(pinia)
app.mount(&#39;#app&#39;)

<!-- language: lang-css -->

#app { line-height: 1.75; }
[v-cloak] { display: none; }

<!-- language: lang-html -->

&lt;div id=&quot;app&quot; v-cloak&gt;
  useTemplate.temporary.itemList: {{ useTemplate.temporary.itemList }}&lt;br/&gt;
  &lt;button @click=&quot;addItem2()&quot;&gt;Add Item&lt;/button&gt;&amp;nbsp;
  &lt;button @click=&quot;useTemplate.reset()&quot;&gt;Reset&lt;/button&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/vue-demi@0.13.11/lib/index.iife.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/pinia@2.0.30/dist/pinia.iife.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月8日 17:20:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定