So I want to apply the class .testcolor to the div when testvalue is true and apply nothing when it's false.
When i add the getClass method to :class it doesn't even get called but it gets called when called from {{ getClass }}. I tried clearing the cache and even rewrote the entire code and it still doesnt work! Here is the full code:
<!DOCTYPE html>
<html>
<head>
<title>Vue Test</title>
<style>
.testcolor {
color: red !important;
}
</style>
</head>
<body>
<div id="test" :class="getClass">
<h1>Test stuff</h1>
<h2>{{ testvalue }}</h2>
</div>
<script type="module">
import { createApp } from "https://unpkg.com/[email protected]/dist/vue.esm-browser.js";
createApp({
data() {
return {
testvalue: true,
};
},
methods: {
getClass() {
console.log("Method 'getClass' called");
return this.testvalue ? "testcolor" : "";
},
},
}).mount("#test");
</script>
</body>
</html>
I figured something out that if I mount the Vue instance on the div element and add the :class="testClass" (from the answer) to the h2 it works! But when I mount it on the h2 element now it doesn't work!