Computed Properties
Now we're going to use computed properties to make our code more readable and reusable.
In here we're going to create a computed property to add the name of the product with the brand name.
First lest add the brand name to the data model.
Now let's create the computed property.
Now our javascript complete code ends up like this:
The computed property allows us to use this component automates way to update the title when the brand or the product changes.
Now let's use the computed property in our html.
Done! Now we have the title of the product with the brand name.
Complex computed properties
Now let's create a complex computed property to show the inStock status of the product.
For this we're going to change the code, first lets change the html to update the way we deal with the variants.
As we can see we're using the index of the variant to update the image.
Now let's change the javascript code to use the index to update the image.
First lets remove the image from the data model and replace it with the selectedVariant, starts with 0 because the first variant is the default one.
Now we remove the inStock, because we're going to use the selectedVariant to get the inStock status.
Let's go to the methods and remove the updateImage to use the selectedVariant.
Now let's create the computed property to get the image and the inStock status.
Now we have the image and the inStock status of the product, when we mousse over the color box.