Home > OS >  The style doesn't apply when adding new class in vue.js
The style doesn't apply when adding new class in vue.js

Time:04-18

I am self-studying Vue.js. I am using v-bind to add the highlight class to the span element, which is supposed to add newBorder style to the element, but the style doesn't get applied.

<!DOCTYPE HTML>
<html>

<head>
    <title>Intro to v-bind</title>
    <meta charset="UTF-8">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        .container {
            background: #cecece;
        }
        .newBorder {
            border: 5px solid yellow;
        }
    </style>
</head>

<body>

    <div id="app">

        <img v-bind:src="'assets/images/look.jpg'" v-bind:alt="'illustration of the word -Look-'" v-bind:title="'Look'">
        <span v-bind:>
            look at me!
        </span>
    </div>

    <script>
        var app = new Vue({
            el: '#app',
            data: {
                loadClass: 'container',
                highlight: 'newBorder'
            }
        });
    </script>
</body>

</html>

Appreciate it if you can point out my mistake. Thanks

CodePudding user response:

Just change the class binding the array syntax instead of object :

 <span v-bind:>

<!DOCTYPE HTML>
<html>

<head>
    <title>Intro to v-bind</title>
    <meta charset="UTF-8">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        .container {
            background: #cecece;
        }
        .newBorder {
            border: 5px solid yellow;
        }
    </style>
</head>

<body>

    <div id="app">

        <img v-bind:src="'assets/images/look.jpg'" v-bind:alt="'illustration of the word -Look-'" v-bind:title="'Look'">
        <span v-bind:>
            look at me!
        </span>
    </div>

    <script>
        var app = new Vue({
            el: '#app',
            data: {
                loadClass: 'container',
                highlight: 'newBorder'
            }
        });
    </script>
</body>

</html>

  • Related