1

I'm getting the following error when trying to implement vue-router.

Unknown custom element: <router-view> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

Where do I need to provide the name option?

A lot of the tutorials I'm looking at seem to be an older version of vue-router. I follow the set-up process but can't get it to work.

Might there be something special I have to do when using the webpack cli template?

I'm also using the vue-router cdn.

Here's my main.js

import Vue from 'vue'
import App from './App'
import ResourceInfo from '../src/components/ResourceInfo'
var db = firebase.database();
var auth = firebase.auth();

const routes = [
  { path: '/', component: App },
  { path: '/info', component: ResourceInfo }
]

const router = new VueRouter({
  routes
})

/* eslint-disable no-new */
var vm = new Vue({
  el: '#app',
  components: { App },
  created: function() {
    firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
            // Get info for currently signed in user. 
            console.log(user);
            vm.currentUser = user;
             console.log(vm.currentUser);
        } else {
            // No user is signed in.
        }
    })
    // Import firebase data 
    var quizzesRef = db.ref('quizzes');
    quizzesRef.on('value', function(snapshot) {
      vm.quizzes = snapshot.val();
      console.log(vm.quizzes);
    })

    var resourcesRef = db.ref('resources');
    resourcesRef.on('value', function(snapshot) {
      vm.resources.push(snapshot.val());
      console.log(vm.resources);
    })

    var usersRef = db.ref('users');
    usersRef.on('value', function(snapshot) {
      vm.users = snapshot.val();
      console.log(vm.users);
    })
  },
  firebase: {
    quizzes: {
        source: db.ref('quizzes'),
        asObject: true
    },
    users: {
        source: db.ref('users'),
        asObject: true
    }, 
    resources: db.ref('resources')
  },
  data: function() {
    return {
        users: {},
        currentUser: {},
        quizzes: {},
        resources: []
    }
  },
  router,
  render: h => h(App)
})

And my App.vue

<template>
  <div id="app">
    <navbar></navbar>
    <resource-info :current-user="currentUser"></resource-info>
    <router-view></router-view>
  </div>
</template>

<script>
import Navbar from './components/Navbar'
import ResourceInfo from './components/ResourceInfo'

export default {
  name: 'app',
  props: ['current-user'],
  components: {
    Navbar,
    ResourceInfo
  }
}
</script>

<style>
</style>
3
  • Before you start typing your question, you may write <!-- language-all: lang-js --> which formats the code inside. It looks nice and improves readability. I have provided my answer below, which has syntax highlighted code. For more info, check out meta.stackoverflow.com/editing-help#syntax-highlighting Commented Oct 25, 2016 at 3:26
  • Thanks I didn't know that. I'll use it next time! Commented Oct 25, 2016 at 3:32
  • I edited your question, with that syntax definition line at the top. No other changes anywhere, and now your code is formatted inside. It is a useful trick I also learnt recently. Commented Oct 25, 2016 at 11:07

3 Answers 3

1

In your main.js file, you need to import VueRouter as follows:

import Vue from "vue"               // you are doing this already
import VueRouter from "vue-router"  // this needs to be done

And below that, you need to initialize the router module as follows:

// Initialize router module
Vue.use(VueRouter)

Other than the above, I cannot find anything else missing in your code, it seems fine to me.

Please refer to the installation page in docs, under NPM section: http://router.vuejs.org/en/installation.html

Sign up to request clarification or add additional context in comments.

4 Comments

I'm importing Vue-router in a script tag in my html.
What about Vue.use(VueRouter)? That still needs to be done inside the main.js file. To do the above, you need to import again in main.js file, so that the dependency is resolved.
The installation page says "You don't need to do this when using global script tags." - which is for Vue standalone build. I believe you are using runtime-only build using webpack template or equivalent, right?
Adding Vue.use(VueRouter) seems to have solved it. Thanks!
0

First install vue router by using "npm install vue-route" and follow the bellow in main.js

import Vue from 'vue'
import Router from 'vue-router'
import App from './App'
import ResourceInfo from '../src/components/ResourceInfo'
var db = firebase.database();
var auth = firebase.auth();

Vue.use(Router)
var router = new Router({
  hashbang: false,
  history: true,
  linkActiveClass: 'active',
  mode: 'html5'
})

router.map({
   '/': {
    name: 'app',
    component: App
  },
  '/info': {
    name: 'resourceInfo',
    component: ResourceInfo
  }
})
// If no route is matched redirect home
router.redirect({
  '*': '/'
});

// Start up our app
router.start(App, '#app')

This might be solve your problem

1 Comment

Just wanted to note that this looks right, but is relevant for vue-router 0.x only. There's a different API in v2.
0

You forgot to import vue-router and initialize VueRouter in main.js

import VueRouter from "vue-router"

Vue.use(VueRouter)

Comments

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.