Home > Blockchain >  $nuxt is not defined when using $nuxt.route in computed
$nuxt is not defined when using $nuxt.route in computed

Time:08-10

I'm looking to use the following for my nuxt routes within the computed property. All seemed to be working until 3mins ago when I started getting $nuxt is not defined. Removing the following code makes the app run again. What am I doing wrong here?

computed: {
    updateActiveRoute() {
        return $nuxt.$route.params.name ? $nuxt.$route.params.name : 'Hello'
    }
},

watch: {
    updateActiveRoute(newVal) {
        this.activeRoute = newVal
    }
}

Thanks

CodePudding user response:

$nuxt, precisely window.$nuxt is only available on client-side and should only be used as escape hatch.

You can access the route object via this.$route instead when using the Options API.

computed: {
    updateActiveRoute() {
        return this.$route.params.name ? this.$route.params.name : 'Hello'
    }
},

watch: {
    updateActiveRoute(newVal) {
        this.activeRoute = newVal
    }
}

CodePudding user response:

Use this.$nuxt

Only within the template you can access variables without this.

  • Related