0

I've tried to override VueJS component method from another component.

Here I want to override checkForm method from another VueJS file.

inside ./src/components/auth_form.vue:

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
  props: {...},
  methods: {
    checkForm: function(e: any) {
      console.log('Please override this method!')
    }
  }
})
</script>

I want to override this method from ./src/views/signup.vue:

<script lang="ts">
import Vue from 'vue'
import authForm from '../components/auth_form.vue'

export default Vue.extend({
  name: 'signup',
  components: {
    authForm
  },
  data: function() {...}, 
  methods: {
    // This does not override the method above!
    checkForm: function(e: any) {
      console.log("success")
      // Reset the form
      this.signupForm = {} as SignupForm
    }
  }
})
</script>

1 Answer 1

2

Yes they will not override because the 2 functions are in 2 different scopes. They can have same name and works independently. In order to do what you want, you have to put the checkForm function of auth_form inside its props.

So in auth_form:

props: {
  checkForm: {
    type: Function,
    default(e) {
      console.log('Please override this method!')
    }
  }
}

then when calling it in signup.vue

<auth-form :check-form="checkForm" />  //pass parent's method into children props

methods: {
  checkForm() {
    console.log("success") // this will override the default one
  }
}
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.