VueJS How can I use computed property with v-for

后端 未结 4 1948
天命终不由人
天命终不由人 2020-11-29 21:12

How can I use computed property in lists. I am using VueJS v2.0.2.

Here\'s the HTML:

4条回答
  •  余生分开走
    2020-11-29 21:29

    Maybe add another v-for that iterates through a one-item-long list:

    Not nice, but that's what you're looking for: an object around that span that has a property called fullName that contains that specific value.

    And it's not just a vanity feature, because we may need to use that value at more than one place, eg.:

    I am {{fullName}}
    You are {{fullName}}
    Who is {{fullName}}?
    

    My use case was that I was constructing dates in v-for loops (yes, another calendar), like:

    
        
            
        
    
    

    (For brevity I omitted the :key="whatever" settings)

    I admit that the nicest way would be to move that to a separate component, but if we create a new component for every two-liner like this, and use that component only at this single place, then we just pollute another namespace.

    Maybe a v-let="day as new Date(...)" directive would be handy for such purpose.

提交回复
热议问题