简体   繁体   English

在 Nativescript Vue 中更改 Fontawesome 图标

[英]Change Fontawesome icon in Nativescript Vue

I'm creating a voting system.我正在创建一个投票系统。 For both positive and negative votes, i use the same component - VoteButton.对于正面和负面投票,我使用相同的组件 - VoteButton。 How to change the icon ( 'fa-thumbs-o-up' to 'fa-thumbs-up' and 'fa-thumbs-o-down' to 'fa-thumbs-down') when the button is activated?当按钮被激活时,如何更改图标('fa-thumbs-o-up' 到 'fa-thumbs-up' 和 'fa-thumbs-o-down' 到 'fa-thumbs-down')? Which is the best option - via CSS (:before), vue.js logic ( IF or something), replace string via JS or something else?哪个是最佳选择 - 通过 CSS (:before)、vue.js 逻辑(IF 或其他)、通过 JS 或其他方式替换字符串?

Parent component:父组件:

<GridLayout columns="auto,*">
   <VoteButton col="0" :buttontext=" 'fa-thumbs-o-up' | fonticon"  :votes="upVotes" label="Upvote" :active="upvoted" width="33%" @click="vote(1)" />
   <VoteButton col="1" :buttontext=" 'fa-thumbs-o-down' | fonticon" :votes="downVotes" label="Downvote" :active="downvoted" width="33%" @click="vote(-1)" />
</GridLayout>

Parent component CSS:父组件 CSS:

.upvote.active {
        background: orangered;
        color: white;
 }

.downvote.active {
        background: blue;
        color: white;
 }

VoteButton child component: VoteButton 子组件:

<template>
  <button @tap="$emit('click')" :class="[buttonClass, { active }]">
    <FormattedString>
        <Span class="fa" > {{ buttontext }}</Span>
        <Span> {{ votes }}</Span>            
    </FormattedString>
  </button>  
</template>

<script>

export default {
    props: {
        active: Boolean,
        label: String,
        buttontext: String,
        votes: Number
    },

  computed: {
    buttonClass() {
      return this.label.toLowerCase();
    }
  }

};
</script>

You could use a counter in javascript to keep track of whether the button should be displaying an upvote or a downvote based on the number of times the button is pressed.您可以在 javascript 中使用计数器来根据按钮被按下的次数来跟踪按钮是应该显示赞成票还是反对票。

Example:例子:

Html:网址:

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
This is a DIV element.
</div>

CSS: CSS:

<style>
.mystyle {
 background: orangered;
 color: white;
 }
 .mystyle2 {
 background: blue;
 color: white;
 }
 </style>

Javascript: Javascript:

<script>
var x = 0;
function myFunction() {
var element = document.getElementById("myDIV");
if (x % 2 == 0){
element.classList.add("mystyle");
element.classList.remove("mystyle2");
x++; 
}
else {
element.classList.add("mystyle2");
element.classList.remove("mystyle");
x++;
}
}
</script>

Thanks!谢谢! I did it the following way:我是通过以下方式做到的:

Parent:家长:

<VoteButton col="0" noactivefatext='fa-thumbs-o-up' activefatext='fa-thumbs-up' :votes="upVotes" label="Upvote" :active="upvoted" @click="vote(1)" />
<VoteButton col="1" noactivefatext='fa-thumbs-o-down' activefatext='fa-thumbs-down' :votes="downVotes" label="Downvote" :active="downvoted" @click="vote(-1)" />

VoteButton component:投票按钮组件:

<template>
  <button @tap="$emit('click')" :class="[buttonClass, { active }]">
    <FormattedString>
      <Span class="fa body" :text="active ? activefatext : noactivefatext | fonticon" ></Span>
      <Span> {{ votes }}</Span>            
    </FormattedString>
  </button>  
</template>    

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

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