2

I am trying to redirect and pass data from one component to another using vue-router.

In my main component I have a click link where I am calling a JavaScript function to do the routing.

<a href="javascript:void(0);" @click="switchComponent('ToolDetail')">click here</a>

methods: {
   switchComponent(comp) {
   this.$router.push({name:comp});
},

As of now the routing itself is giving console error when the link is clicked.

Uncaught TypeError: Cannot read property '_router' of undefined

In my main.js I have already imported my router-

import Vue from 'vue'
import App from './App.vue'
import router from 'vue-router'

Vue.config.productionTip = false
export const changeRoute = new Vue();
new Vue({
  render: h => h(App)
}).$mount('#app')

Vue.use(router)

Edit: Based on answer from @ittus, I did something like this-

In my main.js-

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

Vue.config.productionTip = false

new Vue({
  render: h => h(App)
}).$mount('#app')

In my HomeContent.vue-

<template>
    ...
    ..
    <a href="javascript:void(0);" @click="switchComponent('ToolDetail')">click here</a>
    ...
    ..
</template>
<script>
import ToolDetail from './ToolDetail.vue';
import VueRouter from 'vue-router';

...
..

methods: {
   switchComponent(comp) {
      const routes = [
        { path: '/ToolDetail', component: ToolDetail, name: 'ToolDetail' }
      ]
      const router = new VueRouter({
        routes
      })
      router.push({name:'ToolDetail'});
  },

...
..
</script>

Now, when I click the link nothing happens and no console error. Any help with this please? Let me know if I can provide more details.

1
  • Are you sure - HomeContent.vue exporting this in App.vue file ?, also make sure you are using export in HomeContent.vue correctly Commented Jul 16, 2018 at 9:39

1 Answer 1

2

You need to pass router data to Vue component

import Vue from 'vue'
import App from './App.vue'
import Router from 'vue-router'

Vue.config.productionTip = false
export const changeRoute = new Vue();

Vue.use(Router) // should place it before new Vue()

// just example
const router = new Router({
  routes: [
    {
      path: '/',
      name: 'ToolDetail',
      component: ToolDetail // place your component here
    }
  ]
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

You can check more details in Getting Started document

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

3 Comments

My App.vue is the main component which will load other components. Also, I am importing the vue-rouer in my main.js which has the new Vue({..}). How can import ToolDetail component and create the routes: [] based on that?
Updated the post based on your answer. Please check.
You need to set up routers in main.js component, not in HomeContent.vue. Not that in main.js, you need to pass router: new Vue({ router, render: h => h(App) }).$mount('#app')

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.