Vuejs 有条件地附加数据属性

fef*_*efe 4 conditional-statements custom-data-attribute vue.js

我尝试在循环中将数据属性值有条件地添加到我的 vue 列表中,然后尝试以下操作

<ul data-parent="{{model.parent_id !== null ? model.parent_id : 0}}"></ul>
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,列表不再呈现,如果转储到 html 标签{{model.parent_id !== null ? model.parent_id : 0}}之外,我会看到正确的输出

Geo*_*nov 7

:在此之前使用,我会创建一个这样的计算属性。

computed: {
     parentId() {
       if (this.model.parent_id !== null)
          return this.model.parent_id
       return 0;
     }

}

<ul :data-parent="parentId"></ul>
Run Code Online (Sandbox Code Playgroud)