1

I'm trying to access the closeSlidingItems method of the IonList component so that the sliding item is closed automatically once I click on a button which appears from behind after sliding this item to the right.

I tried to do it by putting a reference to IonList, then accessing it from the callback method for the click event on this button. However, I get an error:

Cannot read property 'closeSlidingItems' of undefined

This is the code in the root component:

<template>
  <ion-app>
    <ion-list ref="myIonList">
      <ion-item-sliding v-for="user in users" :key="user">
        <ion-item-options side="start">
          <ion-item-option v-on:click="favorite(user)" color="primary">
            <ion-icon slot="icon-only" :icon="heartOutline"></ion-icon>
          </ion-item-option>
        </ion-item-options>

        <ion-item :color="userState(user)">
          <ion-label slot="start">{{ user.name }}</ion-label>
          <ion-label slot="end">{{ user.age }}</ion-label>
        </ion-item>
      </ion-item-sliding>
    </ion-list>
  </ion-app>
</template>

<script>
import {
  IonApp,
  IonContent,
  IonList,
  IonLabel,
  IonItem,
  IonItemSliding,
  IonItemOptions,
  IonItemOption,
  IonIcon,
} from "@ionic/vue";
import { defineComponent } from "vue";
import { heartOutline } from "ionicons/icons";

export default defineComponent({
  name: "App",
  components: {
    IonApp,
    IonList,
    IonLabel,
    IonItem,
    IonItemSliding,
    IonItemOptions,
    IonItemOption,
    IonIcon,
  },
  data() {
    return {
      heartOutline,
      users: [
        {
          name: "John",
          age: 35,
          favorite: false,
        },
        {
          name: "Jane",
          age: 30,
          favorite: false,
        },
      ],
    };
  },
  methods: {
    favorite(user) {
      const favorite = user.favorite || false;
      user.favorite = !favorite;
      //console.log(this.$refs.myIonList);
      // This won't work despite I defined a reference to IonList and closeSlidingItems is a method of this component!
      this.$refs.myIonList.closeSlidingItems();
    },
    userState(user) {
      return user.favorite ? "success" : "";
    },
  },
});
</script>

And to try it by yourselves (try sliding one list item to the right and clicking on the heart icon):

https://codesandbox.io/s/ionic-vue-refs-2-msjj6?file=/src/App.vue

Could anyone help me find the issue and how to fix it, or work around it?

2 Answers 2

7
+50

Okay, I think I got it. Try adding $el after the ref

this.$refs.myIonList.$el.closeSlidingItems()

Sign up to request clarification or add additional context in comments.

2 Comments

Wonderful! It works! Thank you! I didn't see that in the docs. It's stated to use this.$refs.<component>.method() Where did you find out?
This solved the problem. Where and how did you find it out? These things are not clear enough from the docs.
2

Same for ion-item-sliding close method. See below simplified example. It looks like the component methods are not visible when you try to get the component instance using Vue3. Ref returns a Proxy object in that case.

https://codesandbox.io/s/ionic-vue-refs-2-forked-zx3lg?file=/src/App.vue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.