英文:
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>
当我检查控制台时,它显示:
我将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('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);
}
}
})
But when I tried to push item into temporary.itemList
in my vue file, it failed:
<script setup lang="ts">
// ...
const useTemplate = useTemplateStore();
// both of them failed
useTemplate.addItem({ ... });
useTemplate.temporary.itemList.push({ ... });
</script>
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>
<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('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')
<!-- language: lang-css -->
#app { line-height: 1.75; }
[v-cloak] { display: none; }
<!-- language: lang-html -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论