2

I am trying to develop a SPA. I installed Vue router at the time of installing Vue-cli. My main.js file is like below

import Vue from 'vue'
import App from './App'
import Router from './router'
import VueResource from 'vue-resource'
import Auth from './packages/auth/Auth.js'


Vue.use(Auth)
Vue.use(VueResource)


Vue.config.productionTip = false

Router.beforeEach((to, from, next) => {
    if(to.matched.some(record => record.meta.requiresAuth)) {
        if(Vue.auth.isAuthenticated()) {
            next({
                path: '/dashboard'
            })
        } 
        else {next()}
    }
    else if(to.matched.some(record => record.meta.forVisitors)) {
        if(!Vue.auth.isAuthenticated()) {
            next({
                path: '/'
            })
        }
        else {next()}
    }
    else {next()}
})


/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    template: '<App/>',
    components: { App }
})

I am getting error like below

enter image description here

Why I am getting error ?

UPDATE @Bert for you.

If I change my main.js file like below

import Vue from 'vue'
import App from './App'
import Router from './router'
import VueResource from 'vue-resource'
import Auth from './packages/auth/Auth.js'

Vue.use(Auth)
Vue.use(VueResource)

Vue.config.productionTip = false

Router.beforeEach((to, from, next) => {
    if(to.matched.some(record => record.meta.requiresAuth)) {
        if(Vue.auth.isAuthenticated()) {
            next({
                path: '/dashboard'
            })
        } 
        else {next()}
    }
    else if(to.matched.some(record => record.meta.forVisitors)) {
        if(!Vue.auth.isAuthenticated()) {
            next({
                path: '/'
            })
        }
        else {next()}
    }else {next()}
})

/* eslint-disable no-new */
new Vue({
      el: '#app',
      router: Router,
      template: '<App/>',
      components: { App }
})

I am getting error like below

enter image description here

What is the solution in this regard ? Is it a problem of router ?

1 Answer 1

1

You import Router with a capital R.

import Router from './router'

You are trying to add it to Vue as router with a lower case r.

new Vue({
    el: '#app',
    router,
    template: '<App/>',
    components: { App }
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Bert. I made an update. I changed the code but I am getting another error. Thanks
@abuabu You might try posting that as a separate question. It's a different issue.

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.