[英]Cannot access Ionic component method from ref (Vue3)
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.我正在尝试访问IonList组件的closeSlidingItems方法,以便在单击将该项向右滑动后从后面出现的按钮后,滑动项会自动关闭。
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.我试图通过引用 IonList 来做到这一点,然后从该按钮上单击事件的回调方法访问它。 However, I get an error:
但是,我收到一个错误:
Cannot read property 'closeSlidingItems' of undefined
无法读取未定义的属性“closeSlidingItems”
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 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?谁能帮我找到问题以及如何解决它,或者解决它?
Okay, I think I got it.好吧,我想我明白了。 Try adding
$el
after the ref尝试在 ref 之后添加
$el
this.$refs.myIonList.$el.closeSlidingItems()
Same for ion-item-sliding
close
method.与
ion-item-sliding
close
方法相同。 See below simplified example.请参见下面的简化示例。 It looks like the component methods are not visible when you try to get the component instance using Vue3.
当您尝试使用 Vue3 获取组件实例时,组件方法似乎不可见。 Ref returns a Proxy object in that case.
在这种情况下,Ref 返回一个代理 object。
https://codesandbox.io/s/ionic-vue-refs-2-forked-zx3lg?file=/src/App.vue https://codesandbox.io/s/ionic-vue-refs-2-forked-zx3lg?file=/src/App.vue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.