0

I am trying to use global information from a mixin. I intend to access the getNow computed prop in a component, but it seems to be undefined.

main.js:

Vue.mixin({
    data: function() {
        return {
          chainBoxURL: "http://172.22.220.197:18004/jsonrpc"
        }
    },
    computed: {
        getNow() {
          const today = new Date();
          const date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
          const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
          const dateTime = date + ' ' + time;
          return dateTime;
        }
    }
})

Component:

methods: {
    getChainAddress(form) {
        if (form.password == form.password_again && form.password != '') {
            console.log(this.getNoW)
        }
    }
}
3
  • What is happening? Are you receiving an error? What is the output of console log? Where are you including the mixin in the component? This isn't enough information to accurately determine what you need. Commented Jun 27, 2020 at 1:42
  • Thanks for listening. I need the getNoW method inside a client.vue component. However it is shown undefined. Commented Jun 27, 2020 at 1:47
  • 2
    The prop is defined as getNow, but your component tries to access getNoW (w/uppercase W). Commented Jun 27, 2020 at 2:16

2 Answers 2

1

Seems like there is a typo in when you try to access getNow, there is a W instead of a w.

Side notes,

  1. You could use template strings to make life slightly easier
const today = new Date();
const date = `${today.getFullYear()}-${(today.getMonth() + 1)}-${today.getDate()}`;
const time = `${today.getHours()}:${today.getMinutes()}:${today.getSeconds()}`;
const dateTime = `${date} ${time}`;
  1. You can flip your conditions within the if statement as JS won't evaluate the 2nd one if the first one is false in case of an &&
if (form.password != '' && form.password == form.password_again) {
  console.log(this.getNoW)
}
Sign up to request clarification or add additional context in comments.

Comments

0

The computed prop in the mixin is defined as: getNow() but youn spelt it as getNoW() within the component.

Either that or you may have forgot to include the mixin in the component.

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.