N
N
Nik2017-07-04 14:08:59
JavaScript
Nik, 2017-07-04 14:08:59

Vue.js and window is not defined?

I use nuxt
on the page in data like this

data () {
      return {
        curTrail:  window.location.pathname.split('/')[2]
      }
    },

when I get the url, I send it to VUEX
created() {
      this.$store.dispatch('getOneProjects', this.curTrail);
    },

when switching between pages, everything works fine, but if you go to this page directly, an error occurs: ReferenceError: window is not defined
Please tell me what is the problem? thank)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2017-07-04
@KoiLVeD

Nuxt has two-way rendering, when we request a page directly - rendering happens on the server, and node.js does not have a window object
, use context.route

data (context) {
      return {
        curTrail:  context.route.route.path.split('/')[2]
      }
    },

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question