programing

해당 계산된 속성 방법 내에서 계산된 속성의 현재 값을 읽는 방법?

prostudy 2022. 5. 6. 19:42
반응형

해당 계산된 속성 방법 내에서 계산된 속성의 현재 값을 읽는 방법?

문자열이 3자 이상이면 쿼리를 활성화하고 싶다.조회가 활성화된 후에는 조회가 활성화된 상태를 유지해야 한다.Vue 2 Composition API를 사용하여reactive쿼리 상태의 개체:

import { computed, defineComponent, reactive, ref } from '@vue/composition-api'

export default defineComponent({
  setup() {
    const truckId = ref<string>('')
    const driverId = ref<string>('')

    const queryEnabled = reactive({
      driver: false,
      truck: false,
    })

이제 값을 설정하십시오.queryEnabled.drivertrue할 때driverId길이가 3자 이상인 문자열로 다음 작업을 수행할 수 있음:

    const queryEnabled = reactive({
      driver: computed(() => driverId.value.length >= 3),
      truck: false,
    })

이것은 작동하지만 또한 설정된다.queryEnabled.driverfalse일단 문자열의 문자가 줄어들면어떻게 하면 우리가 할 수 있을까?computed다음과 같은 속성:

  • 로 시작하다.false
  • 값을 로 설정하다true일단 조건이 충족되면.
  • 의 가치를 유지하다true이전에 설정했다가 다시 변경하지 않는 경우

이것은 오직 하나의 안에서만 이루어질 수 있는가?computed의 재산.reactive물체?하고 생각하고 있었다.function접근하기 위해 뚱뚱한 화살 대신this현재에 있어서computed재산은 있지만 알아낼 수 없다.

당신은 그 집에 접근할 수 없다.computed그 자체에서 얻은 재산이니, 사용하라.watch에 기초하여 상태를 업데이트하다driverId:

import { watch, defineComponent, reactive, ref } from '@vue/composition-api'

export default defineComponent({
  setup() {
    const truckId = ref<string>('')
    const driverId = ref<string>('')

    const queryEnabled = reactive({
      driver: false,
      truck: false,
    })

    watch(driverId,(newVal)=>{
      if(!queryEnabled.driver && newVal.length >= 3){
        queryEnabled.driver = true
      }
    })

참조URL: https://stackoverflow.com/questions/65248740/how-to-read-the-current-value-of-a-computed-property-within-that-computed-proper

반응형