How to bind to attribute in Vue JS?

2019-06-17 01:59发布

I got this error

Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div id="{{ val }}">, use <div :id="val">.

on this line

<a href="/Library/@Model.Username/{{myVueData.Id}}">

It works in Angular 1. How do you do it in Vue?

标签: vue.js
3条回答
祖国的老花朵
2楼-- · 2019-06-17 02:18

In your template:

<a :href="href">

And you put href in data:

new Vue({
  // ...
  data: {
    href: 'your link'
  }
})

Or use a computed property:

new Vue({
  // ...
  computed: {
    href () {
      return '/foo' + this.someValue + '/bar'
    }
  }
})
查看更多
Evening l夕情丶
3楼-- · 2019-06-17 02:29

you can either use the shorthand : or v-bind

<div>
    <img v-bind:src="linkAddress">
</div>

new Vue({
        el: '#app',
        data: {
            linkAddress: 'http://i3.kym-cdn.com/photos/images/newsfeed/001/217/729/f9a.jpg'
        }
});

or for when you need more than just binding an attribute you can also do:

    new Vue({
            el: '#app',
            data: {
                finishedLink: '<a href="http://google.com"> Google </a>' 
            }
    });
查看更多
爷、活的狠高调
4楼-- · 2019-06-17 02:31

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

:href="'/Library/@Model.Username' + myVueData.Id"

and

:id="'/Library/@Model.Username' + myVueData.Id"

Update Answer

Some directives can take an “argument”, denoted by a colon after the directive name. For example, the v-bind directive is used to reactively update an HTML attribute:

<a v-bind:href="url"></a>

Here href is the argument, which tells the v-bind directive to bind the element’s href attribute to the value of the expression url. You may have noticed this achieves the same result as an attribute interpolation using href="{{url}}": that is correct, and in fact, attribute interpolations are translated into v-bind bindings internally.

查看更多
登录 后发表回答