Skip to content
Advertisement

Vue Component, assign computed property to data

i’m trying to set a component’s data to a computed property value, as this fetches some localStorage data and manipulates it slightly.

I would then once the component is mounted listen for changes within the localStorage, and if my key is updated then fetch this value again, run it through my computed property and pass it back to the view.

However i’m getting the following error:

JavaScript

This is my component:

JavaScript

Edit:

Updated my code, getting the same error however when the storage change event is fired.

Edit 2:

Turns out my template was expecting toggle within my scope but I removed this from my $scopedSlots.

Advertisement

Answer

Computed properties work on data/props, so you can’t use them in data itself.

Instead, just set the default value of the data key to what’s in local storage:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement