【问题标题】:Vuejs Apollo I have a problem to retrieve an id in arrayVuejs Apollo 我在检索数组中的 id 时遇到问题
【发布时间】:2021-05-20 11:47:58
【问题描述】:

我正在使用 apollo 和 Vuejs 的突变来从我的日历中删除一个事件的条目。我需要在数组中检索事件 id。


    deleteEvent: function () {
      this.$apollo
        .mutate({
          mutation: MUTATION_entreePlanningDelete,
          variable: {
            id: this.id,
          },
        })

        .then((data) => {
          console.log(data);
        });
    },

在这个函数中我呼吁突变请求。

这是我的数组(这个名字:evenements),我们可以看到 id。你能告诉我如何找回它吗?

【问题讨论】:

    标签: vue.js vue-apollo


    【解决方案1】:

    试试这两个选项:

    第一个索引

    console.log(data[0].id);
    

    或者如果它给出未定义的或其他的东西

    然后尝试直接从对象中抓取它

    let { id } = data[0];
    console.log(id);
    

    也可以尝试不使用索引

    【讨论】:

      【解决方案2】:

      我已经测试了这个选项:

      console.log(evenements[0].id);

      我得到了 id,我已经测试了你的第二个选项

      让 { idEvent } =evenements[0].id; console.log(idEvent);

      idEvent 未定义,但在这个版本中没问题

      让 idEvent = evenements[0].id; console.log(idEvent);

      evenements 它是一个数组,但不可能在其他地方恢复它的值

      【讨论】:

        【解决方案3】:

        我的问题解决了

        我没注意,我们只要把event=参数放到日历中选中的事件中加上.id就可以说检索事件id了。

                   here is what had to be done on the template side
        
          <v-btn text @click="deleteEvent(selectedEvent)"> Effacer </v-btn>
        
        
                  and here is what had to be done in the delete function
        
        
        
        deleteEvent: function (event) {
        
          this.$apollo
            .mutate({
              mutation: MUTATION_entreePlanningDelete,
              variables: {
                id: event.id,
              },
              refetchQueries: [{ query: QUERY_entreesPlanning }]
            })
        
            .then((data) => {
              console.log(data);
            });
        },
          
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-24
          • 1970-01-01
          • 2021-02-08
          • 2019-12-04
          • 1970-01-01
          • 2016-09-18
          相关资源
          最近更新 更多