如何访问 <script setup>
内的计算属性在 vue 中?
我检查了documentation但它甚至没有提到如何在 <script setup>
中定义计算属性,我只从 https://stackoverflow.com/a/71459106/2311074 得到这个
给出下面的代码,如何访问计算属性 url
里面<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'
}
最佳答案
您是否尝试使用url.value
,computed返回一个只读响应式(Reactive)引用对象。
关于javascript - 如何访问脚本设置中的计算属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72606723/