Vue: combining :class="myclass" and class="{{myclass}}" not working

Created on 25 Apr 2016  路  6Comments  路  Source: vuejs/vue

When combining the two to methods below, only the first is rendered:

<p class="page-{{mypage}}" :class="{'page-option': hasoption}">Some page</p>

Is this a bug, or what would be best practice?

Fiddle: https://jsfiddle.net/cg801yp5/

1.0.21

Most helpful comment

@smolinari it does! You're right, thanks!

(the link didn't work for me before)

The solution:

<p :class="['page-' + mypageClass, {'page-option': hasoption}]">Some page</p>

All 6 comments

Not sure if this is what you are looking for, but it's my best guess.

https://jsfiddle.net/cg801yp5/1/

Scott

The official guide states that:

Although you can use mustache interpolations such as class="{{ className }}" to bind the class, it is not recommended to mix that style with v-bind:class. Use one or the other!

So please try to avoid doing so, because the outcome is not guaranteed.

Ah, overlooked that, sorry.

Though, I would vote this to be possible in the future?

@kvdmolen

Wouldn't the solution I offered work for you?

Scott

@smolinari it does! You're right, thanks!

(the link didn't work for me before)

The solution:

<p :class="['page-' + mypageClass, {'page-option': hasoption}]">Some page</p>

Make sure to use dev build, it already warns you against such usage: https://jsfiddle.net/1zuw6be5/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gkiely picture gkiely  路  3Comments

seemsindie picture seemsindie  路  3Comments

robertleeplummerjr picture robertleeplummerjr  路  3Comments

6pm picture 6pm  路  3Comments

hiendv picture hiendv  路  3Comments