Vue: method is not a function within inline-templa

2019-05-11 03:59发布

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.

1条回答
Anthone
2楼-- · 2019-05-11 04:25

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 a getTemplate method on the <ais-results> component, but not finding it.

In your case, instead of directly calling getTemplate, you could emit an event with the result 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 a card-click event (@click="$emit('card-click', result)"). The <ais-results> tag has a listener for that event (@card-click="getTemplate"), so when the card-click event is fired, the getTemplate method will be called with the result data being passed automatically.

<ais-results :results-per-page="10" inline-template @card-click="getTemplate">
  <div class="row">
    <div class="col-6" v-for="result in results.slice(0, 5)" :key="result.objectID">
      <div class="card" @click="$emit('card-click', result)">
        ...
      </div>
    </div>
  </div>
</ais-results>
查看更多
登录 后发表回答