如何使用参数并从VUE 3中的Apollo客户端正确执行USELAZYQUERY?

发布于 2025-02-01 23:58:23 字数 1498 浏览 4 评论 0原文

我正在使用Vuejs(Coption Api语法)学习Apollo客户端,我得到了一个查询,可以通过名称字段搜索用户,而我要做的是用@click Event 在输入旁边的按钮上,您应该在其中编写用户名的名称。 因此,基本上,我正在尝试使用 uselazyquery 并将名称道具作为查询中的参数传递,但我不知道该怎么做。

<div class="selectedUser">
<template>
  <div class="container">
      <input 
        type="text"
        placeholder="Brian..."
        v-model="userSearched"
         />
         <p>{{userSearched}}</p>

         <button
           @click="load()">
         Search for a user
         </button>
      </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, reactive, toRefs } from 'vue';
import { useLazyQuery, useQuery } from '@vue/apollo-composable'
import gql from 'graphql-tag';

export default defineComponent({
  name: 'HomeView',
  setup(){
const GET_USER_BY_NAME = gql`
      query getUser($name: String!){
        user(name: $name) {
          id
          name
          age
          username
          nationality
      }
    },
    `
const state = reactive({
      userSearched : ''
    });

     const {
        result: userSearhedResult,
        load      
        } = useLazyQuery(GET_USER_BY_NAME);
 
    
    ` return {
        users,
        error,
        loading,
        load, 
        ...toRefs(state)
       }
  }});
</script>

如您所见,我的想法是/是使用Reactive()的用户搜索用作param,但我做得不正确。希望您与我分享正确的方法。谢谢

I'm learning Apollo client with VueJs (Composition API syntax), I got a query that searches a user by the name field, and What I'm trying to do is to trigger the fetch with a @click event on a button next to an input in which you should write the user's name.
So basically I'm trying to use useLazyQuery and pass the name prop as param in the query but I don't know how to do it.

<div class="selectedUser">
<template>
  <div class="container">
      <input 
        type="text"
        placeholder="Brian..."
        v-model="userSearched"
         />
         <p>{{userSearched}}</p>

         <button
           @click="load()">
         Search for a user
         </button>
      </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, reactive, toRefs } from 'vue';
import { useLazyQuery, useQuery } from '@vue/apollo-composable'
import gql from 'graphql-tag';

export default defineComponent({
  name: 'HomeView',
  setup(){
const GET_USER_BY_NAME = gql`
      query getUser($name: String!){
        user(name: $name) {
          id
          name
          age
          username
          nationality
      }
    },
    `
const state = reactive({
      userSearched : ''
    });

     const {
        result: userSearhedResult,
        load      
        } = useLazyQuery(GET_USER_BY_NAME);
 
    
    ` return {
        users,
        error,
        loading,
        load, 
        ...toRefs(state)
       }
  }});
</script>

As you can see, my idea is/was to use the userSearched from the reactive() as param, but I'm not doing it right. Hope you share with me the correct way to do it. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文