How to solve Interpolation inside attributes has been removed. Use v-bind or the colon shorthand ? Vue.JS 2

samuel toh picture samuel toh · Apr 4, 2017 · Viewed 42.5k times · Source

My vue component is like this :

    <template>
        <div>
            <div class="panel-group" v-for="item in list">
                ...
                <div class="panel-body">
                    <a role="button" data-toggle="collapse" href="#purchase-{{ item.id }}" class="pull-right" aria-expanded="false" aria-controls="collapseOne">
                        Show
                    </a>
                </div>
                <div id="purchase-{{ item.id }}" class="table-responsive panel-collapse collapse" role="tabpanel">
                ...
                </div>
            </div>
        </div>
    </template>
    
    <script>
        export default {
            ...
            computed: {
                list: function() {
                    return this.$store.state.transaction.list
                },
                ...
            }
        }
    </script>

When executed, there exist error like this :

Vue template syntax error:

id="purchase-{{ item.id }}": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead.

How can I solve it?

Answer

Happyriri picture Happyriri · Apr 4, 2017

Use javascript code inside v-bind (or shortcut ":") :

:href="'#purchase-' + item.id"

and

:id="'purchase-' + item.id"

Or if using ES6+:

:id="`purchase-${item.id}`"