我正在學習 javascript 和 vue.js 3 組合 API。我的問題是我只是想獲得一個陣列長度并在
. 陣列名稱:“getForms”
<script>.....
const forms_length = computed(() => getForms.value.length)
<template>....
<p> {{form_length}} </p>
我收到一個錯誤“未捕獲(承諾)TypeError:無法讀取未定義的屬性(讀取'長度')”
為什么?我應該怎么做?
感謝您的幫助!
uj5u.com熱心網友回復:
您應該以這種方式使用計算屬性
<template>
<p>Array length: {{ formsLength}}</p>
</template>
<script>
import { computed } from 'vue'
import {useFormsStore} from '../store/forms'
setup() {
const { store } = useFormsStore()
// if the store.forms array is undefined or not ready,
// then it returns an empty array
const getForms = computed(() => { return store.forms || []})
const formsLength = computed(() => getForms.value.length)
return {
formsLength
}
}
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/478072.html
標籤:javascript 数组 Vue.js vue-composition-api
