Using more than one class at a time is causing issues

Essentially, I have a div and I want to create an exercise where I apply three different classes using vue.js. I attempted to use v-bind:class, specifically :class, but can I bind a class directly from CSS?

This is what I tried:

html

<div :style="[myClass1, myClass2, myClass3]">I have no class applied :( </div>

css

.myClass1 {
  background-color: black;
  width: 200px !important;
  height: 200px !important;
}

.myClass2 {
  border: 2px solid red;
}

.myClass3 {
  padding: 2px 2px 2px 2px;
}

Javascript

new Vue({
  el: '#exercise',
  data: {
    effect: true,
  },
  methods: {
    startEffect: function() {
      var vm = this;
      setInterval(function(){
        vm.effect = !vm.effect;
        console.log(this.effect);
      },1000);
    }
  }
});

If I cannot bind the classes directly, how can I achieve this with vue.js?

Answer â„–1

Refer to the resources for information on how to use array syntax for dynamic classes, you must utilize v-bind:class in your code.

new Vue({
  el: '#exercise',
  data: {
    effect: true
  },
  methods: {
    startEffect: function() {
      var vm = this;
      setInterval(function(){
        vm.effect = !vm.effect;
        console.log(this.effect);
      },1000);
    }
  }
});
.myClass1 {
  background-color: red;
  width: 200px !important;
  height: 200px !important;
}

.myClass2 {
  border: 2px solid red;
}

.myClass3 {
  padding: 2px 2px 2px 2px;
}
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="exercise">
<div v-bind:class="['myClass1','myClass2','myClass3']">I got no class :(</div>
</div>

Answer â„–2

In HTML, you assign styles using the style attribute and classes using the class attribute. However, in the given example, classes are incorrectly being assigned to the style attribute.

Furthermore, the classes listed do not correspond to any values in the JavaScript example provided. To address this issue, either include them in the data or enclose them in quotes like so:

<div :class="['myClass1','myClass2','myClass3']">Yay, classes!</div>

For more information, refer to the documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Execute the function when the control becomes visible

Currently, I possess an input control: <input id="someID" class="form-control" type="text" name="SomeData" data-ng-model="vm.SomeData"> I have a requirement to set the initial value of vm.SomeData upon user scrolling down to this control. As a begi ...

Match one instance of a character in a regular expression pattern while simultaneously matching two occurrences of a different character

I am attempting to create a function that can match one occurrence of a certain character and two occurrences of another character simultaneously. For instance, in the string "_Hell_o", I want to match the first underscore (_) and in the string "++Hello", ...

Crash in the Android WebView of the Galaxy S3 device due to Signal 11 SIGSEGV

My Android WebView is experiencing issues with memory access and crashes on a Galaxy S3 running Android 4.0.4. The HTML5 content includes interactive battles where enemies appear for the user to slash, interspersed with normal HTML pages. Despite the use o ...

Exploring the Applications of Directives in Multiple Modules within Angular 2

When I declare the directive in two modules, I get an error that says Type PermissionDirective is part of the declarations of 2 modules. However, when I declare it in only one module, I receive an error stating Can't bind to 'isPermission' s ...

Is it possible to execute a URL twice instead of just once in AngularJS?

While developing a web application with AngularJS and Rest Web services, I encountered a problem where the URL is being executed twice instead of just once. I have created a service function for executing the web service API and calling it from the control ...

Why don't inline style changes implemented by JavaScript function properly on mobile browsers like Chrome, Dolphin, and Android?

View the jsFiddle here Issue on PC/Windows browser: When performing action, h1 header "gif" disappears and video starts playing. Problem on mobile devices: The gif does not disappear as expected but the video still plays indicating that JavaScript is fun ...

Achieving a Photo Grid Layout with CSS

Looking for help with my CSS photogrid - it's close to what I want, but not quite there. Here's the layout I'm aiming for: 1 | 2 | 3 4 | 5 | 6 But currently, it displays like this: 1 | 3 | 5 2 | 4 | 6 I've tried tweaking the CSS but ...

Incorporating an HTML page into a dialog using AngularJS: A step-by-step guide

Is it possible to dynamically change the content of a dialog by updating the URL? I have multiple HTML pages that I want to display in my dialog by simply changing the URL. <md-button ng-click="showDialog($event,'myurl/page-1.html')">Show ...

Issues with Pagination in Laravel and Vue

After clicking on page 2 of the pagination component, the application receives data for page=2, but it does not display on the screen. Instead, the pagination reverts to page 1 and everything starts over. I am utilizing bootstrap-vue as my UI component lib ...

Troubleshooting problem with GZIP in Angular 2 application deployment

I have developed an Angular 2 TypeScript application. I am using Firebase for hosting and Cloudflare for optimizing speed, caching, and security. When checking the browser header, it indicates: accept-encoding:gzip, deflate, sdch, br The app.js file has ...

How do you use CSS to replace an HTML "rules" attribute?

When publishing to HTML using the DITA Open Toolkit, certain inline table attributes are automatically applied such as frame="border" and rules="all". I am facing an issue where I need to override the "rules" attribute for table cells using CSS styles. Wh ...

Struggling to remove the image tag from the jQuery ajax response

My web app is designed to send an ajax post request to a PHP script, which then returns a chunk of HTML data. This HTML includes an image and a table of information. The challenge I'm facing is how to extract the image from the rest of the HTML so tha ...

What is the best way to update the state by either adding to it or replacing it if the key

I'm currently working on a project involving radio buttons and I need to create dynamic state by setting the initial state on the first click, then updating it with subsequent clicks by either concatenating the new value onto the existing state or rep ...

What issues can be identified in this ajax.js script?

This particular function is designed to verify the existence or availability of a user login, yet even when the user database table is empty, the form consistently indicates that there is a duplicate entry. As a result, the submit button disappears. What c ...

Displaying a Nuxt Vue component according to a specified parameter or type

As a newcomer to Nuxt and Vuejs, I have been scouring the web for a solution to my issue without any luck. The problem I am facing is with a page containing components <VTextField></VTextField> and <VSwitch></VSwitch>. Below is a s ...

Build React separately with embedded fonts included

My React JS project currently utilizes Google Fonts through URL imports in SASS files, but now I need to create a version that can be used on a standalone local network without internet access. This means the fonts must be included within the project. I a ...

Styling the checked state of a switch in NativeScript/Angular with ngModel/FormBuilder

Let's explore the styling of a switch element within Angular: <Switch class="switch" formControlName="answer"></Switch> One approach involves targeting the switch with checked attribute, setting its background-color and text color accord ...

The persistent space between the navbar and the index page remains despite the suggested fixes provided

There appears to be a discrepancy between the navigation bar and the rest of the page. Despite my efforts to adjust margins, I haven't been able to resolve it. The system is prompting me for more details before posting this question, but I'm unsu ...

Resolving the "Error: Cannot update a React state on an unmounted component" issue

I encountered a console error while attempting to navigate to a new page within my web application. Here's the error message I received: Warning: A React state update was attempted on an unmounted component, which is essentially a no-op. However, t ...

I am encountering difficulties displaying the image and CSS files from another folder in my HTML with Node.js

I am facing difficulty in establishing a connection between my front-end and back-end using node.js. As a result, the website only displays the HTML code without linking to the CSS or image files. Here is the folder structure: root -src •pi ...