vue.js - Vue Font-awesome 图标连绑定(bind)

标签 vue.js

我有一些类似于西蒙游戏的代码。当使用按钮作为箭头时,使用 @mousedown 事件一切正常。我想用很棒的字体图标来美化它,但在我换掉 <button> 后保持外观不变。对于 <icon> @mousedown 事件停止触发。其他一切似乎都一样。

来 self 的模板:

<div id="top-row">
  <icon name="arrow-left" class="but-0" v-bind:class="{ active: pushed0 }" aria-hidden="true" @mousedown="toneButtonPushed(0)"></icon>
  <icon name="arrow-left" class="but-1" v-bind:class="{ active: pushed1 }" aria-hidden="true" @mousedown="toneButtonPushed(1)"></icon>
</div>

toneButtonPushed 甚至在这里:

methods: {
    toneButtonPushed: function (buttonPushed) {
      console.log('hit')
      if (this.compTurn === false) {
        if (buttonPushed === this.compTones[this.playerTone]) {
          const toneName = 'simonSound' + buttonPushed.toString()
          this.$refs[toneName].play()
          if (this.playerTone === this.compTones.length - 1) {
            if (this.compTones.length === this.winLevel) {
              this.msg = 'YOU WIN!!!!'
              setTimeout(() => {
                this.initGame()
              }, 2500)
            } else this.toggleTurn()
          } else {
            this.playerTone++
          }
        } else {
          if (this.strict === true) {
            this.$refs.wrong.play()
            this.initGame()
          } else {
            this.$refs.wrong.play()
            this.compTurn = true
            this.showTones()
          }
        }
      } else {
        this.$refs.wrong.play()
      }
    },

我有一些模糊的感觉它与组件的导入方式有关,所以我也包括了导入语句。如果您需要更多代码,完整的项目在这里。 https://github.com/CliffSmith25/simon

import Icon from 'vue-awesome/components/Icon.vue'

export default {
  name: 'app',
  data: function () {
    return {
      compTones: [],
      playerTone: 0,
      compTurn: true,
      intervalID: '',
      strict: false,
      winLevel: 20,
      pushed0: false,
      pushed1: false,
      pushed2: false,
      pushed3: false,
      msg: ''
    }
  },
  components: {
    Icon
  },

最佳答案

这有点棘手,因为标准事件不适用于自定义组件,因为当您执行以下操作时:

<mycomponent @click="method"></mycomponent>

组件正在寻找从另一个组件发出的事件,它不知道您的意思是在好的、旧的 DOM 点击事件上。

一个选项是从子组件发出点击事件,在您的情况下为 Icon.vue,但这不是最佳解决方案

还有一个,它是事件的.native修饰符,像这样:

<div id="top-row">
  <icon name="arrow-left" class="but-0" v-bind:class="{ active: pushed0 }" aria-hidden="true" @mousedown.native="toneButtonPushed(0)"></icon>
  <icon name="arrow-left" class="but-1" v-bind:class="{ active: pushed1 }" aria-hidden="true" @mousedown.native="toneButtonPushed(1)"></icon>
</div>

通过这样做,您是在说您要使用来自 DOM 的标准 mousedown 事件的组件,并且它不会查找发出的事件。

演示:http://jsbin.com/sanivevuxa/edit?html,js,console,output

关于vue.js - Vue Font-awesome 图标连绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43020880/

相关文章:

javascript - 如果索引为 0,Vue.js 如何添加事件类

laravel - [Vue warn] : Unknown custom element: <app> - did you register the component correctly?(在 <Root> 中找到)

javascript - 如何在Vue.js中计算部分值和总值?

javascript - 在 Vue + Webpack 中使用 Puppeteer (Headless Chrome) 时的依赖错误

vue.js - Vuetify 主题不变

javascript - HTML 中的 Vue JS 无法识别添加的对象和属性

vuejs2 - 如何动态地将事件添加到我的自定义网格组件中的标签

javascript - 如何将类 ="active"放入 vuejs for 循环中的第一个元素

vue.js - 在 CI 管道中运行开发服务器

sorting - 排序表 Vue.js