0

currently i am rewriting my Vue 3 application to use the Composition API and I have a problem rewriting

methods: {
  isDataValid() {
    if (this.$refs.data.$refs.cost.enabled) {
      return this.$refs.data.$refs.cost.$refs.module.dataValid();
    }
  }
}

From the Vue Official Template Refs Docs I know I have to create a ref and assign it to the corresponding component but I do not understand how to do it in my special case with nested refs. The child components still use the Options API, how can I archive the rewriting for my case?

1
  • 1
    I would rethink how this is done. Parent directly accessing data of Child or Grandchild component is not good practice. A change in the child's code code break the code in the parent, for example. I think they made this type of coding practice harder to accomplish in Composition API on purpose. It's better for child components to emit data, or use something like global store, e.g. pinia Commented Apr 8, 2024 at 16:49

1 Answer 1

0

I just rewrite the parent component,I think you can rewrite the children follow this.

<child ref="data"/>
...
const data = ref()
function isDataValid(){
if (data.value.$refs.cost.enabled) {
      return data.value.$refs.cost.$refs.module.dataValid();
    }
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.