0

Can I make a custom element tapable?

This is my component:

<template>
  <FlexboxLayout class="profile-item" flexDirection="column">
    <label :text="text" />
    <label class="subtext" v-if="subtext" :text="subtext" />
  </FlexboxLayout>
</template>

And this is how I would like to use it:

<template>
  <ScrollView>
    <StackLayout>
      <Item text="Test" @tap="onItemTap" />
      <Button text="Button" @tap="onItemTap" />
    </StackLayout>
  </ScrollView>
</template>

<script>
import Item from "./Item";

export default {
  components: {
    Item
  },
  methods: {
    onItemTap(event) {
      alert('test');
    },
  }
};
</script>

Taping the buttons works but not my custom element.

1 Answer 1

1

You can either handle the tap event from inside the custom element or wrap the custom element inside a ContentView and attach the tap event to the container like this:

<template>
  <ScrollView>
    <StackLayout>
      <ContentView @tap="onItemTap">
        <Item text="Test" />
      </ContentView>
      <Button text="Button" @tap="onItemTap" />
    </StackLayout>
  </ScrollView>
</template>

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

7 Comments

Thank you. Where did you find the ContentView component? It is not documented here: nativescript-vue.org/en/docs/introduction . Is it possible to use ListView (nativescript-vue.org/en/docs/elements/components/list-view) with static content?
There are some info about ContentView here. I think ListView is more used for dynamic contents compared to static content.
Thank you. ContenView and tap is working fine. Clicking on a dynmaic ListView content gives a "tab" feedback on the element. Can I do this also somehow with ContentView?
I think those are the default behavior of buttons and ListView. It is possible, but you would need to create your own custom gesture handler. It's probably easier to use a ListView/button if you want that feedback.
Do you know if I can use ListView somehow with static content (not coming from an array, instead I want to write my elements directly into my template [Like my example with a Stacklayout]) ?
|

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.