programing

계산된 속성에서 오류가 반환되는 이유

prostudy 2022. 3. 14. 21:42
반응형

계산된 속성에서 오류가 반환되는 이유

사용하고 있다.Vuex, 안에Getter Foo function배열 내에서 두 개의 값을 반환하는 중:

return ["Try Again"]또는return ["Data result", data]계산된 결과, 나는array length그리고 결과에 따라 되돌아온다.

  computed:{    
    Foo: function(){
      const getFoo =  this.$store.getters.Foo;
      if(getFoo.length === 1) {
        this.existFoo = false
        return getFoo[0]
      }
      this.existFoo = true
      return getFoo
    }
  }

하지만 나는 이런 실수를 하고 있다. 심지어 다른 글을 읽어도 나는 그것을 해결할 수 없다.

34:9 오류 "Foo" 계산 속성 vue/비부작용 계산 속성에서 예기치 않은 부작용
37:7 오류 "Foo" 계산 속성 vue/비부작용 계산 속성에서 예기치 않은 부작용

당신은 컴퓨터 사용의 상태를 변경할 수 없다.다음 대신 다른 계산된 컴퓨터를 사용해 보십시오.existFoo

  computed:{        
    Foo(){
      if(this.$store.getters.Foo.length === 1) {
        return this.$store.getters.Foo[0]
      }          
      return this.$store.getters.Foo
    },
    existFoo(){
        return this.$store.getters.Foo.length > 1
    }
  }

이제 제거하십시오.existFoo로부터state

감시자를 사용하여 저장 값을 감시하고 로컬 변수를 설정할 수 있다.

computed: {
  getFooFromStore() {
    return this.$store.getters.Foo
  }
}

watch: {
  getFooFromStore: function() {
    this.existFoo = this.getFooFromStore[0] ? false : true;
  }
}

참조URL: https://stackoverflow.com/questions/66235491/why-am-i-getting-error-return-in-computed-property

반응형