如何正确使用 v-bind (vue.js)

Posted

技术标签:

【中文标题】如何正确使用 v-bind (vue.js)【英文标题】:How to use v-bind properly (vue.js) 【发布时间】:2019-06-21 12:06:45 【问题描述】:

我有一个Vue.component,它在表中创建一行:

 Vue.component('comment-row', 
                props: ['comment'],
                template: '<tr>' +
                    '<th>comment.authorName</th>' +   // works fine
                    '<th>comment.value</th>' +  // also works fine
                    '<th><form action="/remove_comment/comment.id">' +  // problem here
                    '<button type="submit">X</button></form></th>' +
                    '</tr>'
            );

该行看起来像:

作者 |一些留言 |按钮“X”以独特的操作删除此行

这里有一个问题: Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead.

好的,我们按照我们的要求去做:

<form v-bind:action="/remove_comment/comment.id">

但是这里出现了另一个问题: Invalid regular expression flags in

我得到的是字符串comment.id而不是数字

问题是如何在Vue.js中的html标签action = ""中正确使用['comment'] prop?

【问题讨论】:

【参考方案1】:

你可以像这样使用 v-bind:

<form v-bind:action="'/remove_comment/' + comment.id"></form>

或者使用computed property 来获得更简洁的代码 v-bind:action="mycomputedProperty"

【讨论】:

【参考方案2】:

伊万回答的简写版本。

来自vue.js的参考docs

<form :action="'/remove_comment/' + comment.id"></form>

【讨论】:

以上是关于如何正确使用 v-bind (vue.js)的主要内容,如果未能解决你的问题,请参考以下文章