How to pass ref to method/function parameter without unwrapping the wrapper?


DarkLite1

We're using the Vue Composition API and want to pass refobjects (not just values) as arguments to functions. Some code to clarify:

import { defineComponent, ref } from '@vue/composition-api'

export default defineComponent({
  setup() {
    const driverId = ref()
    const truckId = ref()

    const clearDriverId = () => {
      driverId.value = null
    }

The code above works fine, but we need to replicate the functionality clearDriverIdtoo truckId. That's why we're creating the following:

    const clearField = (field: Ref) => {
      field.value = null
    }

Access from template:

<q-input
  label="Driver ID"
  v-model="driverId"
>
  <template v-slot:append>
    <q-icon
      name="close"
      @click.stop="clearField(driverId)"
    />
  </template>
</q-input>

Of course, this won't work because ref driverIdwhen passed to the function from the template , they are unpacked. What is the correct way to pass a full refobject to a function ?

Tony 19

One solution setup()is to return an object/dictionary containing all of refthem (preventing them from auto-expanding), which can then be used to pass a specific function refto your function. Utilities that toMyRefs()return dictionaries refas well as refdictionaries ( below) will help minimize duplication of code:

<template>
  <div>
    <button @click="clearField(myRefs.driverId)">Clear driverId</button>
    <button @click="clearField(myRefs.truckId)">Clear truckId</button>
    <button @click="clearField(myRefs.carId)">Clear carId</button>
    <button @click="clearField(myRefs.trainId)">Clear trainId</button>

    <pre>
      driverId: {{ driverId }}
      truckId: {{ truckId }}
      carId: {{ carId }}
      trainId: {{ trainId }}
    </pre>
  </div>
</template>

<script>
import { defineComponent, ref } from '@vue/composition-api'

const toMyRefs = refs => ({
  ...refs,
  myRefs: {
    ...refs
  }
})

export default defineComponent({
  setup() {
    const driverId = ref(1)
    const truckId = ref(2)
    const carId = ref(3)
    const trainId = ref(4)

    const clearField = (field) => {
      field.value = 0
    }

    return {
      ...toMyRefs({
        driverId,
        truckId,
        carId,
        trainId,
      }),
      clearField,
    }
  }
})
</script>

demo

Related


JNI Java Wrapper: How to pass byte[] parameter

Lokananayake I need to call a Java API from C++ using JNI. I am trying to pass one byte*as follows: Java void OperateData(byte[] data, int dataLength) { //Some Implementation } C++ void OperateData(byte* data, int dataLength) { JavaMethod* methodObj =

How to pass parameter to member variable by ref?

JS Pretty new to C#, coming from a C++ background a long time ago, so I seem to be having trouble transitioning from pointers to refs in C#. I have a class (EColour) created with the constructor shown. I assign (or at least try to) a reference to cellTemplate

Pass parameter as Wrapper class and override

username Please clarify my doubts about overriding, when I call a method that is not overridden, the called method is the form parent class, please briefly explain this, the example is as follows public class A { public void test(int x){

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass parameter to $ref in OpenAPI 3

some development Suppose I have the following schema for later use $ref: "schemas": { "Order": { "type": "object", "properties": { "id": { "type": "integer", "format": "int64" }, "petI

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass parameter to $ref in OpenAPI 3

some development Suppose I have the following schema for later use $ref: "schemas": { "Order": { "type": "object", "properties": { "id": { "type": "integer", "format": "int64" }, "petI

Python pass ref int as parameter

Mr. Sha The tlb library in my python project passes win32com.client. I've easily used a number of built-in functions, but one of the main functions takes a list of arguments for two of them marked as ref int. When I try to pass a python integer to the function

How to pass return type as parameter (without reflection?)

Jordy Brinks I am trying to create a frame. The purpose of making this framework is to internally parse data from the internet and make it available to users of the framework (API) E.g: The framework has a BasicUser class with (string name, long when created)

How to pass object method as parameter without object

Shetor So I have a situation where I need to call a method of an object, but by the time I call it, the object and the called method may be different. var myCallback = "nameOfMethod"; //this can change throughout function myObj(){ this.method1 = function(

How to pass variable without url parameter in Django?

Ray Smith My question is, how can I pass the information in Django from the template to the view without the url parameter. I know sessions can help me do this, however, I can't find an online resource that shows how to create a key-value pair in a session in

How to pass object method as parameter without object

Shetor So I have a situation where I need to call a method of an object, but by the time I call it, the object and the called method may be different. var myCallback = "nameOfMethod"; //this can change throughout function myObj(){ this.method1 = function(