简体   繁体   English

@click 事件中的问题,它添加了一个 fontawesome 图标

[英]problem in @click event which adding one more fontawesome icon

i tried to make responsive navigation with vue js, when i tried to add '@click' to toggle burger bar or close icon, but it's adding the 'close' icon instead every time i click it.当我尝试添加“@click”来切换汉堡栏或关闭图标时,我尝试使用 vue js 进行响应式导航,但每次单击它时都会添加“关闭”图标。

here's the code这是代码

template section模板部分

<template>    
// ----
    <button @click="toggleButton">
                <i v-if="!toggle" class="fas fa-bars"></i>
                <i v-if="!toggleClose" class="fas fa-times"></i>
              </button>
    // ----

</template>

script section脚本部分

<script>
export default {
  name: "MultiNav",
  data: function() {
    return {
      toggle: false,
      toggleClose: true
    };
  },
  methods: {
    toggleButton() {
      this.toggle = !this.toggle;
      this.toggleClose = !this.toggleClose;
    }
  }
};
</script>

pict:图片: 输出

i try to use:我尝试使用:

 <button @click="toggle = ! toggle">
     <i v-if="!toggle" class="fas fa-bars"></i>
     <i v-if="toggle" class="fas fa-times"></i>
 </button>

it still resulting the same result它仍然产生相同的结果

You need to add a key to the icons.您需要为图标添加一个键。

 <button @click="toggle = ! toggle">
    <i v-if="!toggle" key="bars" class="fas fa-bars"></i>
    <i v-if="toggle" key="times" class="fas fa-times"></i>
 </button>

The key will tell Vue's algorithm that the icons are in fact different elements.关键将告诉 Vue 的算法图标实际上是不同的元素。 Without that the virtual dom representation thinks that they are the same element.没有它,虚拟 dom 表示认为它们是相同的元素。

Try switching the classes using condition尝试使用条件切换类

<button @click="toggle = ! toggle">
     <i  class="fas" :class="toggle?'fa-times':'fa-bars'"></i>
</button>

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

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