Vue component method not being run on event in dyn

2019-07-11 05:55发布

I am attempting to dynamically create some HTML content and bind a Vue component method to an event on the element. However this does not work. Below is a description of the problem and a snippet with a test case.

Steps to reproduce problem

  1. Click Click me 1
  2. Observe the console.
  3. Click Toggle popover for tooltip.
  4. Click Click me 2
  5. Observe the console.

Expected behaviour

In Step 2 you should see "you should see this in the console" in the console. In Step 5 you should see "you should see this in the console" again.

Actual behaviour

In Step 2, "you should see this in the console" appears in the console. In Step 5, "you should see this in the console" does not appear in the console.

new Vue({
  el: '#app',

  methods: {
    expectToSucceed() {
      console.log('you should see this in the console');
    },
  },

  created() {
    $(document).ready(() => {
      const mockedDynamicHtml = `
        <div class="popover">
          <h1>Example tooltip</h1>
          <button @click="expectToSucceed" class="remove">Click me 2</button>
        </div>
      `;

      $('[data-toggle="popover"]').popover({
        html: true,
        template: mockedDynamicHtml,
      });
    });
  },
});
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <title></title>
  </head>
  <body>

    <div id="app">
      <a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Toggle popover for tooltip</a>
      <br><br>
      <p><span @click="expectToSucceed">Click me 1</span></p>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
  </body>
</html>

1条回答
Lonely孤独者°
2楼-- · 2019-07-11 06:19

It will be @click, like this:

<i @click="test"></i> 

as shorthand of v-on is @ not :.

查看更多
登录 后发表回答