Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access computed properties inside scrip setup?

How do you access a computed property inside <script setup> in vue? I checked the documentation but it does not even mention how to define computed properties inside <script setup>, I got that only from https://stackoverflow.com/a/71459106/2311074

So given the below code, how to access the computed property url inside <script setup>?

<script setup>
import {computed} from 'vue';

const url = computed(() => 'Hallo');

const printUrl = () => {
   // How to access here the computed property url?
   
   this.url // throws TypeError "Cannot read properties of undefined" 
   
   url // <- returns an object of ComputedRefImpl instead of 'Hallo'
}
like image 846
Adam Avatar asked Feb 27 '26 14:02

Adam


1 Answers

Did you try with url.value, computed returns a readonly reactive ref object.

like image 67
Nikola Pavicevic Avatar answered Mar 01 '26 04:03

Nikola Pavicevic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!