简体   繁体   中英

Vue Js v-bind:class syntax not working ?

i am new in VueJs and i am having that little problem, first here is my sample HTML code :

<div class="search">

        <input :class="{ longwidth : isActive }" v-show="showInput" type="text">
        <img @click="hideImgShowInput" v-show="hideImg" src="Assets/images/search-icon.png">

</div>

i have followed the documentation exactly, and i am using PHPStorm as editor, but my function that changes the 'isActive' variable is not working i am having this error:

Attribute :class is not allowed here

Any help would be much appreciated.

That sounds like a PHPStorm warning. Ignore it, or try a Vue-aware editor like vs code or atom. Your code looks fine to me.

This error causes the component to not render

My case is similar to the following

<script type="text/x-template" id="game-row">
    <tr>
        <td>{{ title }}</td>
        ...
    </tr>
</script>

This way when adding the class attribute to tr element, causes the mentioned message

<script type="text/x-template" id="game-row">
    <tr :class="{ active: isActive }">
        <td>{{ title }}</td>
        ...
    </tr>
</script>

The way to fix it was to pass the class attribute in the custom component call, as follows

<script type="text/x-template" id="game">
    <div>
        <p v-if="isLoading">Loading...</p>
        <template v-else>
            <table>
                <caption>Game {{ title }}</caption>
                <thead>
                    <tr>
                        <th>Name</th>
                    </tr>
                </thead>
                <tbody>
                    <tr
                        is="game-day-row"
                        v-for="game of games"
                        :class="{ 'active': isActive }" <!-- Here I set the class -->
                        :key="game.id"
                        :game="game"
                    ></tr>
                </tbody>
            </table>
        </template>
    </div>
</script>

You can read about it in the vue class and style guide

https://v2.vuejs.org/v2/guide/class-and-style.html#With-Components

In the html output the class active is added to the tr element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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