0

I'm new to Vue.js and I have created one simple form for the user and storing data using API.

On submit I'm calling this function:

setup(props, { emit }) {
    const blankData = {
      customer: '',
      template: '',
      rate: '',
      property_from: '',
      property_to: '',
      move_date: '',
      num_days: '',
      token: '',
      details: '',
      customer_options: [],
      template_options: [],
      rate_options: [],
      property_from_options: [],
      property_to_options: [],
    }

    const userData = ref(JSON.parse(JSON.stringify(blankData)))
    const resetuserData = () => {
      userData.value = JSON.parse(JSON.stringify(blankData))
    }
    const toast = useToast()

    const onSubmit = () => {
      store.dispatch('app-user/addUser', userData.value)
        .then(
          response => {
            if (response.status === 1) {
              this.$router.push({ name: 'edit-user', params: { id: 10 } })
            }

            toast({
              component: ToastificationContent,
              props: {
                title: response.message,
                icon: response.toastIcon,
                variant: response.toastVariant,
              },
            })
          },
          error => {
            console.log(error)
          },
        )
    }

    const {
      refFormObserver,
      getValidationState,
      resetForm,
    } = formValidation(resetuserData)

    return {
      userData,
      onSubmit,
      refFormObserver,
      getValidationState,
      resetForm,
    }
  },

And trying to redirect the user to the edit page after user creation but I'm getting this error and not redirecting:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'router')

I have tried with this stackoverflow answer but getting same error:

const onSubmit = () => {
    const self = this
    store.dispatch('app-user/addUser', userData.value)
    .then(
        response => {
            if (response.status === 1) {
                self.$router.push({ name: 'edit-user', params: { id: 10 } })
            }
        },
        error => {
            console.log(error)
        },
    )
}

Any idea what I'm doing wrong in my code?

1
  • What is this referring to in that context? Where is your onSubmit function defined, and where is it being used? Need more context. Commented Nov 22, 2021 at 6:25

2 Answers 2

2

I think this might be help

router with composition api https://next.router.vuejs.org/guide/advanced/composition-api.html

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

2 Comments

Yes. I'm using Vuexy - Vuejs theme.
I updated answer after you updated question
1

You're using Vue 3 and a setup function; there is no this in a setup function.

See Accessing the Router and current Route inside setup.

Untested, but probably something like this will work:

setup() {
  const router = useRouter()

  const onSubmit = () => {
    // ... code omitted ...

    router.push({ name: 'edit-user', params: { id: 10 } })
  }

  return {
    onSetup,
    // other stuff...
  }
}

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.