v-for and v-if not working together in vue.js

后端 未结 6 1250
轻奢々
轻奢々 2020-12-16 13:27

A form is used to submit text and two options which tell vue which column to display the text in. When the col2 radio button is checked the submitted text should display in

相关标签:
6条回答
  • 2020-12-16 13:49

    Your second check is !item.col==='two' and would only display if it does not equal 'two'.

    EDIT: The ! not operator is likely binding more closely than === so that will always return false. Add brackets to control the order of application. I say likely because it may be a bit of Vue magic that I'm not familar with, rather than a pure JavaScript expression.

    I think you want to remove that exclamation mark. Or to make it !(item.col==='one') to display for any value other than 'one'.

    0 讨论(0)
  • 2020-12-16 13:50

    If for some reason, filtering the list is not an option, you can convert the element with both v-for and v-if in to a component and move the v-if in to the component.

    Original Example

    Original Loop

    <li v-for="item in info" v-if="item.col==='one'">
      text: {{ item.text }}, col: {{ item.col }}
    </li>
    

    Suggested Refactor

    Refactored Loop

    <custom-li v-for="item in info" :visible="item.col==='one'">
      text: {{ item.text }}, col: {{ item.col }}
    </custom-li>
    

    New Component

    Vue.component('custom-li', {
      props: ['visible'],
      template: '<li v-if="visible"><slot/></li>'
    })
    
    0 讨论(0)
  • 2020-12-16 13:51

    Why don't use the power of Computed Properties ?

    computed: {
      infoOne: function () {
        return this.info.filter(i => i.col === 'one')
      },
      infoTwo: function () {
        return this.info.filter(i => i.col === 'two')
      }
    }
    

    Then on each list just iterate over its respective property without the need to check. Example

    <ol>
       <li v-for="item in infoOne">{{item}}</li>
    </ol>
    

    Here the working fiddle

    0 讨论(0)
  • 2020-12-16 13:57
    <div v-for="item in items">
    
       <div v-if="checkThis(item.computeThisProperty)" />
       <div v-else />
    
    </div>
    
    methods: {
       checkThis(i) {
          return this[i];
       }
    },
    
    computed: {
       myComputedProperty() {
          return this.$store.state.something ? true : false;
       }
    }
    
    0 讨论(0)
  • 2020-12-16 13:59
    <div class="row">
        <div class="col-md-6">
            <ol>
                <li v-for="item in info">
                    <template v-if="item.col==='one'">
                        text: {{ item.text }}, col: {{ item.col }}
                    <template>
                </li>
            </ol>
        </div>
        <div class="col-md-6">
            <ol>
                <li v-for="item in info">
                    <template v-if="!item.col==='two'">
                        text: {{ item.text }}, col: {{ item.col }}
                    <template>
                </li>
            </ol>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-16 14:08

    Remove ! from second if v-if="item.col==='two'"

    better you can do this way (to iterate only once):

    <div class="row" v-for="item in info">
                <div class="col-md-6">
                    <ol>
                        <li v-if="item.col==='one'">
                            text: {{ item.text }}, col: {{ item.col }}
                        </li>
                    </ol>
                </div>
                <div class="col-md-6">
                    <ol>
                        <li v-if="item.col==='two'">
                            text: {{ item.text }}, col: {{ item.col }}
                        </li>
                    </ol>
                </div>
            </div>
    
    0 讨论(0)
提交回复
热议问题