vue.jselectronvue-routervue-clielectron-vue

vue-router should route on click, not working


I am trying to create a navigation and when someone clicks. It should navigate to another Vue.component.

Note: This app is Electron+Vue based, but it should work as normal Vue Project, as i've tried same concept in other Electron+Vue Project too.

Right now it shows no Content because of error of Router. Am i missing anything?

Error Shows like this in Screenshot

===================================

----HelloWorld.vue-----

<template>
   <div class="collapse navbar-collapse nav-item-outer" id="navbarNav">
     <ul class="nav navbar-nav nav-list">
        <li class="nav-item active" @click="navigate('information')">
          <span class="nav-item-icon nav-item-icon-info"></span>
                <span class="nav-item-title">Info</span>
        </li>
        <li class="nav-item" @click="navigate('map')">
          <span class="nav-item-icon nav-item-icon-location"></span>
                <span class="nav-item-title">Location</span>
        </li>
        <li class="nav-item" @click="navigate('videocontent')">
          <span class="nav-item-icon nav-item-icon-video"></span>
                <span class="nav-item-title">Video</span>
        </li>
        <li class="nav-item" @click="navigate('surfstation')">
          <span class="nav-item-icon nav-item-icon-surf"></span>
                <span class="nav-item-title">Surf</span>
        </li>
        <li class="nav-item" @click="navigate('telephone')">
          <span class="nav-item-icon nav-item-icon-telephone"></span>
                <span class="nav-item-title">Telefon</span>
        </li>
            <li class="nav-item" @click="navigate('/')">
                <span class="nav-item-icon nav-item-icon-home"></span>
                <span class="nav-item-title">Home</span>
            </li>
      </ul>
    </div>
</template>
<script>
  export default {
    name: 'HelloWorld',
    methods: { 
      navigate(route) {
          this.$router.push(route)                
      },
    }
  }
</script>

===================================================

----main.js-------

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

 Vue.config.productionTip = false
Vue.use(VueRouter)




export default new VueRouter({
  routes: [
   {
     path: '/',
     name: 'dashboard',

     component: require('@/views/Dashboard').default
  },
  {
    path: '/information',
    name: 'information',

    component: require('@/views/Information').default
  },
  {
    path: '/map',
    name: 'map',

    component: require('@/views/Map').default
  },
 {
    path: '/surfstation',
    name: 'surfstation',

    component: require('@/views/Surfstation').default
  },
  {
    path: '/telephone',
    name: 'telephone',

    component: require('@/views/Telephone').default
  },
  {
    path: '/videocontent',
    name: 'videocontent',

     component: require('@/views/VideoContent').default
  },
  {
    path: '*',
    redirect: '/'
  }
  ]
})



new Vue({
    render: function (h) { return h(App) },
}).$mount('#app')

=====================================================

----App.vue------

<template>
  <div id="app">

   <HelloWorld msg="Welcome to Your Vue.js App"/>
       <main>
         <router-view></router-view>
       </main>
     </div>
  </template>

 <script>
    import HelloWorld from './components/HelloWorld.vue'

     export default {
      name: 'App',
      components: {
        HelloWorld
      }
    }
 </script>

A help would be Appreciated.


Solution

  • Instead of exporting the router, put in a const and specify it inside new Vue({

    main.js

    const router = new VueRouter({
      routes: [
        ...
      ],
      // Prevent white screen on reload (in production build)
      mode: process.env.IS_ELECTRON ? 'hash' : 'history'
    })
    
    
    new Vue({
      router,
      render: h => h(App),
      mounted () {
        // Prevent white screen when the app is launched (in production build)
        this.$router.push('/').catch(error => { })
      }
    }).$mount('#app')