<script setup> import { ref } from 'vue' const foo = ref(1) function changeSth(sth, val) { sth.val = val; // an error trigger, because ref object is unwrapped, sth is just a plain value } </script> <template> <button @click="changeSth(foo, 2)">click me change sth.</button> </template> Quiero pasar el objeto ref foo a la función changeSth 。 para poder reutilizar changeSth para hacer algo como changeSth(foo) changeSth(bar) . Pero ahora solo puedo obtener el valor del objeto ref. Entonces, cómo pasar el objeto ref en sí mismo al controlador de eventos como changeSth con sintaxis de plantilla.
Intenta lo siguiente:
<template> <button @click="changeSth('foo', 2)">click me change sth.</button> {{ foo }} </template> <script setup> import { ref } from 'vue' const foo = ref(1) function changeSth(sth, val) { this[sth] = val } </script>