0

I have an url: http://localhost:8080/Currency?currency=RMB I want to get the currency param which is RMB

In:

created(){
    this.currencyParam = this.$route.query.currency;
    console.log(curr: ${this.currencyParam});
}

and I can get the curr: RMB in F12 - console but in F12 -Vue I get currency:undefined

In my template:

<template v-else>
          <gateway
            :currency="this.$route.query.currency"
          />
</template>

I get an error:

Error in render: "TypeError: Cannot read property '$route' of undefined found in and in F12 -Vue I still get currency:undefined

6
  • No this in templates - just $route... Commented Aug 27, 2019 at 4:56
  • @Estradiaz I know it can solve, but why? Commented Aug 27, 2019 at 5:01
  • Some insight into reactivity stackoverflow.com/questions/57615156/… Commented Aug 27, 2019 at 5:38
  • The template parser got designed this way - as an example on v-on stackoverflow.com/questions/57524110/… Commented Aug 27, 2019 at 5:39
  • @Estradiaz can you help me with this pls?stackoverflow.com/questions/57676629/… Commented Aug 27, 2019 at 14:47

1 Answer 1

0

You can add a watch property that will allow you to listen changes in query param

data () {
  return {
   currencyParam = null
  }
},
watch: {
    '$route.query.currency': function () {
       if(this.$route && this.$route.query.currency) { // if not undefined
            console.log(`curr: ${this.$route.query.currency}`);
            this.currencyParam = this.$route.query.currency;
       }
    }
  }

Also change your template like this;

<template>
   <gateway v-if="currencyParam" :currency="currencyParam" />
</template>
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.