I have this component:
<template>
<div class="modal fade modal-primary" id="imageModal" tabindex="-1" role="dialog" aria-labelledby="ImageLabel"
aria-hidden="true">
<div class="modal-dialog modal-lg animated zoomIn animated-3x">
<div class="modal-content">
<ais-index index-name="templates"
app-id="BZF8JU37VR"
api-key="33936dae4a732cde18cc6d77ba396b27">
<div class="modal-header">
<algolia-menu :attribute="category"
:class-names="{ 'nav-item__item': 'nav-color', 'nav-item__link': 'nav-link', 'nav-item__item--active': 'active'}">
</algolia-menu>
</div>
<div class="modal-body">
<div class="container">
<ais-results :results-per-page="10" inline-template>
<div class="row">
<div class="col-6" v-for="result in results.slice(0, 5)" :key="result.objectID">
<div class="card" @click="getTemplate(result)">
<img class="img-fluid" v-lazy="result.image"/>
<div class="card-body">
<p>{{ result.description }}</p>
</div>
<div class="card-footer">
<small>Created: {{ result.created_at }}</small>
</div>
</div>
</div>
<div class="col-6" v-for="result in results.slice(5, 10)" :key="result.objectID">
<div class="card">
<img class="img-fluid" v-lazy="result.image"/>
<div class="card-body">
<p>{{ result.description }}</p>
</div>
<div class="card-footer">
<small>Created: {{ result.created_at }}</small>
</div>
</div>
</div>
</div>
</ais-results>
</div>
</div>
</ais-index>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</template>
<script>
import Algolia from '@/components/algolia/menu';
export default {
components: {
"algolia-menu": Algolia,
},
data() {
return {
category: 'category',
};
},
methods: {
getTemplate(result) {
console.log(result)
}
}
}
</script>
I have a click listener on the .card
div within my <ais-results>
tag which calls my getTemplate
method. But, whenever I click on that element, it produces this error:
imageModal.vue?8d74:85 Uncaught TypeError: _vm.getTemplate is not a function at click (imageModal.vue?8d74:85) at invoker (vue.runtime.esm.js:2023) at HTMLDivElement.fn._withTask.fn._withTask
Why is this happening? I have tried @click.native
as well, but that didn't work.
The issue is that you’re using an inline template for your
<ais-results>
component tag, so the data references within that tag are scoped to the<ais-results>
instance. This means Vue is looking for agetTemplate
method on the<ais-results>
component, but not finding it.In your case, instead of directly calling
getTemplate
, you could emit an event with theresult
data and then listen for the event on the<ais-results>
tag.Below is a simplified example where clicking on the
.card
div in the inline template will emit acard-click
event (@click="$emit('card-click', result)"
). The<ais-results>
tag has a listener for that event (@card-click="getTemplate"
), so when thecard-click
event is fired, thegetTemplate
method will be called with theresult
data being passed automatically.