How to use more than 1 dynamic class with vuejs?

Asked

Viewed 303 times

0

Good morning/afternoon / evening

I have a doubt, I have a project where I have a dynamic class with ternary operators, I need to add another dynamic class to bring an extra class in this whole joke, but return always gives me an error of "syntax error" I can’t make it work because I don’t understand how to bind this operator correctly.

<div :class=" edit ? 'card-servico servico-valor servico-edit' : 'card-servico'" v-show="!tour || ei === 0">

EDIT:

The idea is to add a new dynamic class, in case without interfering with the operation being done on Edit.

data(){
 return {
   servicoPagante: {
      'pudim': true
    },
  }
}

<div v-on:click="edit ? :class=" edit ? 'card-servico servico-valor servico-edit' : 'card-servico', 'servicoPagante' :true " v-show="!tour || ei === 0">

but adding Servicopagante there, it gives me syntax error because prior to it is rolling an operation with ternary that is correct but precise add plus this class for another validation

  • Which classes you want to associate?

  • I wanted to use this service dynamically, but it does not roll because of the previous suit that is rolling

  • Then vc has 3 sets of classes: 1 - card-service-value service-Edit, 2 - card-service and 3 - servicoPagant, Do you want to make the condition so that it is what? Okay confused.

  • Apart from servicePagant, the others work, I want to make servicePagant work without interfering with the previous ones, it would be the new class but it does not work due to the suit. I don’t think I know how to add it without interfering with the previous ones

1 answer

2


Creates a computed Property make this logic. In the Vue API you can return an array with strings and objects (similar to what you are doing in the template), and it would look like this:

In the template:

<div :class="classesCardServico" v-show="!tour || ei === 0">

In the component would be:

computed: 
    classesCardServico(){

     return [
        'card-servico', 
        {'servico-valor': this.edit},
        {'servico-edit': this.edit},
        {'servicoPagante': this.servicoPagante.assinante }
      ];
    }
  • I edited my comment because it was very confused my doubt, the tender operation will roll but I need to add 1 more dynamic class doing another function without interfering directly in this application

  • @leonardosilva what is the logic of servicoPagante? There’s only one "pudding" key? Can there be more keys? Can you explain better?

  • can yes, there will be 1 variable where depending on an API return 1 value true or false will add 1 class or remove this class, in the case of Servicopagante, the idea that the class is dynamic for the return of the api tell whether it is valid or not and whether it can receive 1 style or not

  • @leonardosilva then the value that decides whether the class should be added or not is this. servicoPagante.pudim?

  • yes the pudding is just one way gave keep testing if the class ta working, would be something like this.servicoPagante.free || this.servicoPagante.subscriber

  • @Leonardosilva and the class is only triggered if this.servicoPagante.assinante for true or also if this.servicoPagante.free for true? seem to me mutually exclusive...

  • the idea is that because it is a dynamic class it will add 1 class and in css I will add effects to it, if it is free it will do X if subscriber will do Y, the problem is that by the way it was put in :class="edti ? " he is clashing with these classes and did not want them to run together but in parallel without affecting them, a new class that I put there and does other things and does not influence those that are already there, however I tried to put with { } It gives me syntax error and I can’t isolate the suits

  • @Leonardosilva I realize this, this is common and simple. What I don’t understand is your logic, you have to explain better... then in what condition you want to add class servicoPagante? when this.servicoPagante.assinante for true?

  • Exactly, when I receive from an API and check if an API element is true I will bring the TRUE condition to this.servicoPagante.subscriber and the same will add the class up to my element, if false adds ANOTHER class to the element

  • @leonardosilva ok, for this specific case of this.servicoPagante.assinante add the class servicoPagante if it is true I just added it to the answer. Then it is to do the same for other classes in the same way... the keys of this object will be used as classes of the value of the same keys for true. To be more consistent I suggest you use servico-pagante in the class name, so you keep the style of nomenclature you already have in the other.

  • I didn’t understand, how would I use, 1 methods, 1 date and 1 computed prop? I didn’t understand exactly how to add and use

  • @leonardosilva what method would that be? I see nothing about methods in the question... I think part of the problem is still in your head and not in the question ...

  • @leonardosilva can have a look here: https://jsfiddle.net/Sergio_fiddle/2rkLjzf0/6/ ? adapt to reproduce your problem... have in this example classes, methods, computed and data...

  • Thank you Sergio, I am adapting the logic to be easier to understand, your example helped me a lot.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.