How to define an array to use inside a computed?

I need to have a ref that will hold an array for my Vue 3 app written using composition API and typescript.

The array is used in a computed where I will push in some objects. At the moment I’m unable to achieve this I will get this error on push when I target the array in computed

Property ‘push’ does not exist on type ‘Ref<[]>’

This is the code I’m using:

const myArray = ref<[]>([]);

const myComputed = computed(() => {
  if (propertyOne) {
    myArray.push({ id: 'autoId0', path: 'myPath' })
  }
  return myArray
})

I’ve also tried by using a let myArray = [] inside the computed but will be not populated. Any suggestions?

4

Let’s first address your error of:

Property ‘push’ does not exist on type ‘Ref<[]>’

Consider these examples:

const stringValue: string = ''
const numberValue: number = 123

If you don’t explicitly add the type, typescript will use the implied type based on the initial value i.e

const impliedStringType = ''
const impliedNumberType = 123

That being said if you do:

const myArrayRef = ref([])

The implied type is Ref<[]>

Type Ref is NOT an array but an “object” containing an array.

Docs about Ref:

  • https://vuejs.org/api/reactivity-core.html#ref
  • https://vuejs.org/guide/essentials/reactivity-fundamentals.html#declaring-reactive-state-1

To get the actual value from a ref, you access it via the value property.

Thus to make push work:

myArrayRef.value.push()

Next to address your usage of computed,

Computed usually are derived values and are free from any mutations.

If your derived value is an array with extra value based on a critieria, copy the array, mutate that, and return that instead.

Docs: https://vuejs.org/guide/essentials/computed.html#best-practices

In other words:

const myArrayRef = ref([])

const myComputedArray = computed(() => {
  // Regular array. Not a Ref.
  // A new array with values from the original array.
  const computedArray = [...myArrayRef.value]

  if (criteria) {
    computedArray.push(123)
  }

  return computedArray
})

Otherwise, what you are doing is whenever the criteria is met, the original array gets push.

So if the following scenario happens:

  1. Criteria: true
  2. Criteria: false
  3. Criteria: true

Your original array now has 2 new values because you are mutating the original array.


Here is a simple Vue Playground demo and another using object Vue Playground demo

8

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật