I am trying to scroll through different options that are stored in an array, by using the function below and passing it the correct parameters. But no matter what syntax I try, it doesn’t seem to work.
This is my array and the ref variable i use:
const clothingTypes = ['blazerAndShirt', 'blazerAndSweater', 'collarAndSweater', 'graphicShirt'];
const selectedClothingType = ref(clothingTypes[0]);
I have tried calling the following function in two different ways:
const nextType = (array, refVariable) => {
const currentIndex = array.indexOf(refVariable.value);
const nextIndex = (currentIndex + 1) % array.length;
refVariable.value = array[nextIndex];
};
@click="() => nextType(clothingTypes, selectedClothingType)"
and
@click="nextType(clothingTypes, selectedClothingType)"
But I get the same error in console:
Uncaught TypeError: can’t assign to property "value" on "blazerAndShirt": not an object
What is the correct syntax?
3
Answers
Your error is due to the parameters you pass to your function.
When calling
nextType(clothingTypes, selectedClothingType)
from the vue template, theref
are unwrapped. That means you don’t pass in the ref, but the string value directly:Here is a better and simpler approach to your problem: store the current index of the element you want to show.
In the template, just do:
And it will automatically update the computed property
currentItem
, that you can use in your template.Unfortunately Vue unwrap refs in a template so its value passed as argument rather than the ref itself, you can use an utility to fix that:
See on Vue SFC Playground
Usage:
I don’t see any benefit of passing the state as parameters, just call the function that change the state without any parameters :