简体   繁体   中英

Dynamically set role="status" with vue

I'm looking for a way to set the role="status" attribute using vue.

Something like :role="{'status': isStatus}"

<span class="result-total" role="status">
  <span class="result-number">{{ resultNumber }}</span>
  <label>Results found</label>
</span>

:role="{'status': isStatus}" is special syntax usable only for style and class bindings. For anything else you need to use standard JS bindings.

Computed property is best and most performant solution:

computed: {
  role() { return isStatus ? 'status' : '' }
}
<span class="result-total" :role="role">
  <span class="result-number">{{ resultNumber }}</span>
  <label>Results found</label>
</span>

To set strings based on a boolean you can do something like this:

:role="isStatus ? 'status' : ''"

Change '' to whatever default role you want.

See JavaScript documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM