简体   繁体   English

vuejs - 如何使用点击事件附加 html

[英]vuejs - How to append html with click event

I try to append a couple of buttons with click event when the component is mounted:安装组件时,我尝试在单击事件中附加几个按钮:

      data() {
          return {
            show_word: true, 
            game: {
              //type: "memory, working memory",
              type: 1,
              name: "Symbols",
              start:false,
              total_combo:0,
              total_correct:0,
              total_incorrect:0,
              total_correct_in_a_row:0,
              max_correct_in_a_row:0,
              correct_percent:0,
              avg_time_for_ans:0,
              button: '<button @click="checkNumbers">click</button>',
              score_plus:0,
              score_multi:0,          
              d_timer:600,
              finish:false,
              positive: null,
              negative: null,
              numbers_array:[],
              abuse: {
                clicks:5,
                seconds:1,
                max:3,
                flag: false
              }
            },
          }
        },

methods:{
    playGame(){

          let tilesize = 50, tilecount = 6;

          $( '#numbersContainer' ).empty()

          let gRows = Math.floor($("#numbersContainer").innerWidth()/tilesize);
          let gCols = Math.floor($('#numbersContainer').innerHeight()/tilesize);

          let vals = _.shuffle(_.range(tilecount));
          let xpos = _.shuffle(_.range(gRows));
          let ypos = _.shuffle(_.range(gCols));

          console.log(vals);
          console.log(xpos);
          console.log(ypos);
          let $this = this;

          _.each(vals, function(d,i){
              var $newdiv = $('<button @click="checkNumbers('+(d+1)+')"/>').addClass("tile btn btn-info");
              $this.game.numbers_array.push(d+1)
              $newdiv.css({
                  'position':'absolute',
                  'left':(xpos[i] * tilesize)+'px',
                  'top':(ypos[i] * tilesize)+'px'
              }).appendTo( '#numbersContainer' ).html(d+1);  
          });
        },
        checkNumbers($val){
          console.log($val)

        },
}

This is the html that i getting:这是我得到的 html:

<div id="numbersContainer" class="col-ms-3">
<button @click="checkNumbers(6)" class="tile btn btn-info" style="position: absolute; left: 250px; top: 250px;">6</button>
<button @click="checkNumbers(5)" class="tile btn btn-info" style="position: absolute; left: 150px; top: 150px;">5</button>
<button @click="checkNumbers(1)" class="tile btn btn-info" style="position: absolute; left: 100px; top: 0px;">1</button>
<button @click="checkNumbers(3)" class="tile btn btn-info" style="position: absolute; left: 50px; top: 100px;">3</button>
<button @click="checkNumbers(2)" class="tile btn btn-info" style="position: absolute; left: 200px; top: 200px;">2</button>
<button @click="checkNumbers(4)" class="tile btn btn-info" style="position: absolute; left: 0px; top: 50px;">4</button>
</div>

The problems is that the "click" event is not working.问题是“点击”事件不起作用。 Seems like the "checkNumbers" function is not register, although it registered in my methods.似乎“checkNumbers”函数没有注册,尽管它在我的方法中注册。

What is the right way to append html element with register events?将 html 元素附加到注册事件的正确方法是什么?

Thank !感谢 !

Don't manipulate the DOM directly, that's why you're using Vue in the first place.不要直接操作 DOM,这就是你首先使用 Vue 的原因。 Use a combination of v-for and data() to manipulate state and have Vue populate the DOM.使用v-fordata()的组合来操作状态并让 Vue 填充 DOM。

Adding the @click="" within the direct DOM manipulation is going to cause the element to not be managed by Vue, in other words it won't have the Vue context.在直接 DOM 操作中添加@click=""将导致元素不由 Vue 管理,换句话说,它不会有 Vue 上下文。

 const Game = { props: ["tilecount"], data() { return { tiles: [] }; }, mounted() { this.tiles = _.shuffle(_.range(this.tilecount)); }, methods: { checkNumbers(val) { console.log(val); } } }; const app = new Vue({ el: "#app", components: { game: Game }, data() { return { tilecount: 50 }; } });
 .game .number-container { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; } .game .number-container button { flex-basis: 20%; height: 50px }
 <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script> <div id="app"> <game :tilecount="tilecount" inline-template> <div class="game"> <div ref="numberContainer" class="number-container"> <button v-for="tile in tiles" @click="checkNumbers(tile)"> {{tile}}</button> </div> </div> </game> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM