VueJS conditionally add an attribute for an element

前端 未结 7 1529
旧时难觅i
旧时难觅i 2020-12-02 09:15

In VueJS we can add or remove a DOM element using v-if:


but is there a wa

7条回答
  •  失恋的感觉
    2020-12-02 09:42

    You don't need because if test is truthy you'll already get the required attribute, and if test is falsy you won't get the attribute. The true : false part is redundant, much like this...

    if (condition) {
        return true;
    } else {
        return false;
    }
    // or this...
    return condition ? true : false;
    // can *always* be replaced by...
    return (condition); // parentheses generally not needed
    

    The simplest way of doing this binding, then, is

    Only if the test (or condition) can be misinterpreted would you need to do something else; in that case Syed's use of !! does the trick.
     

提交回复
热议问题