0
votes

I have a vuejs component that bring me a select box. first step I can select the agent ou group, but to see the second select i need to select agent or group first. So when the user don't select the first select option i want to hide the second select, but in my computed property the error is : Error in render: "TypeError: Cannot read property 'length' of undefined"

my template:

  <template>
  <div :class="$style['atribution__container']">
    <select
      item-label="name"
      :label="$t('sidebar.filter.atribution.title')"
      :options="atributionTypes"
      :clear="false"
      :placeholder="placeholder"
      :value="atributionType"
      @input="handleAtributionType"
    ></select>

    <select
      v-if="isAtributionType"
      append-to-body
      item-label="name"
      :clear="false"
      :placeholder="$t('sidebar.filter.atribution.placeholder')"
      :options="attributionsItens"
      :value="selectedAtributionFilter"
      @input="handleAtributionFilterSelection"
    ></select>
  </div>
</template>

script (data, method and computed property):

data() {
      return {
        atributionType: undefined,
        selectedAtributionFilter: undefined,
        selectedAtributionType: undefined,
        isOpen: false,
        atributionTypeDropDownOpen: false,
        ele: []
      }
    },
    computed: {
      ...mapGetters([
        'temporaryFilter',
        'selectedFilter',
        'agents',
        'agent',
        'visibleGroups',
        'hasBots',
        'temporarySelectedFilterName',
        'currentInbox'
      ]),
      placeholder() {
        return this.$te(this.temporarySelectedFilterName)
          ? this.$t(this.temporarySelectedFilterName)
          : this.temporarySelectedFilterName
      },
      atributionTypes() {
        const types = []
        if (this.showAgentFilter) {
          types.push({ name: 'Agente', type: 'agent' })
        }
        if (this.visibleGroups && this.visibleGroups.length) {
          types.push({ name: 'Grupo', type: 'group' })
        }
        return types
      },

     isAtributionType() {
            return !!this.atributionType.length < 0
        }
},
 watch: {
      selectedAtributionIdFilter(id) {
        if (!id) {
          this.atributionType = []

          this.selectedAtributionFilter = []
        }
      },
      atributionType(value) {
        if (!value) {
          this.atributionType = []
          this.selectedAtributionFilter = []
        }
      }
    },
1
If the error is in the computed property, maybe try: return this.atributionType && this.atributionType.length < 0Alessandro
How do you define atributionType?Boussadjra Brahim
@BoussadjraBrahim i updated the question with the codeMatheus Barem
@Alessandro works in the first try but when the watch clean the variables the computed property don't changeMatheus Barem

1 Answers

0
votes

Try to assign atributionType to an empty array

data() {
  return {
    atributionType: [],
    selectedAtributionFilter: null, // If you don't initially know the meaning, 
    selectedAtributionType: null, // you can assign null to the variable.
    isOpen: false,
    atributionTypeDropDownOpen: false,
    ele: []
  }
},