I am struggling with redirecting to another Vue page from my script code. I tried using router.push() but it's not directing me to the desired Vue page.
Here is a snippet of my source code:
src/router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import HomePage from '@/components/HomePage'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'IndexPage',
component: IndexPage
},
{
path: '/homepage',
name: 'HomePage',
component: HomePage
}
]
})
src/components/IndexPage.vue
<script>
import VueRouter from 'vue-router'
export default {
name: 'IndexPage',
methods: {
redirectUser() { // this method is triggered on button click
if (1 == 1)
{
router.push('/homepage');
//this.$router.push('/homepage');
}
}
}
}
</script>
When executing this code, I encounter an error message stating:
ReferenceError: router is not defined at eval
src/main.js
import Vue from 'vue'
import App from './App'
import router from './router'
Vue.config.productionTip = false
window.Vue = Vue
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
Although I can manually access the link from the browser http://localhost:8080/#/homepage, I'm unable to redirect to it from my script code.