Nuxt.js: How use VueRouter instance in the plugin?

Created on 26 Dec 2017  路  5Comments  路  Source: nuxt/nuxt.js

I try use vue-analytics. In plugin i need VueRouter instance like this:

import Vue from 'vue'
import VueRouter from 'vue-router'
import VueAnalytics from 'vue-analytics'

const router = new VueRouter({
  router: // your routes
})

Vue.use(VueAnalytics, {
  id: 'UA-XXX-X',
  router
})

Where can I import a router based on the folder structure?

This question is available on Nuxt.js community (#c2135)

Most helpful comment

@cawa-93 use context https://nuxtjs.org/api/context and route object like this:

import Vue from 'vue'
import VueI18n from 'vue-i18n'

Vue.use(VueI18n)

export default ({ app, route }, inject) => {
  // Set `i18n` instance on `app`
  // This way we can use it in middleware and pages `asyncData`/`fetch`
  app.i18n = new VueI18n({
    /* `VueI18n` options... */
  })
}

you can look here too: https://nuxtjs.org/faq/google-analytics

All 5 comments

You can read here more about how to create a plugin with nuxt
Plugins Guide
and with example here
Plugin Example

Just do some research and you will figure it out otherwise come back here we can help

Yes, I'm familiar with these materials. However, I do not understand how to access the router instance

@@Have you seen this ? https://www.npmjs.com/package/@nuxtjs/google-analytics

@cawa-93 use context https://nuxtjs.org/api/context and route object like this:

import Vue from 'vue'
import VueI18n from 'vue-i18n'

Vue.use(VueI18n)

export default ({ app, route }, inject) => {
  // Set `i18n` instance on `app`
  // This way we can use it in middleware and pages `asyncData`/`fetch`
  app.i18n = new VueI18n({
    /* `VueI18n` options... */
  })
}

you can look here too: https://nuxtjs.org/faq/google-analytics

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bimohxh picture bimohxh  路  3Comments

vadimsg picture vadimsg  路  3Comments

mikekidder picture mikekidder  路  3Comments

lazycrazy picture lazycrazy  路  3Comments

mattdharmon picture mattdharmon  路  3Comments