I'm encountering an issue with
this.$router
being undefined in App.vue and other components. I've been struggling to find a solution.
Here's a snippet of my main.js:
Vue.use(VueRouter);
Vue.use(VueResource);
const router = new VueRouter({
routes: Routes,
mode: 'history'
});
new Vue({
el: '#app',
render: h => h(App),
router
});
And here is my router.js file which I'm importing in main.js:
export default [
{path: '/', component: Home, name: 'home'},
{path: '/home', component: Home, name: 'home2'},
{path: '/user/login', component: Login, name: 'userLogin'}
];
Thank you :)
Edit
Initially, I was attempting to access it within script tags like this:
<script>
import HeadNavBar from './components/HeadNavBar.vue';
import SideBarNav from './components/SideBarNav.vue';
import Home from './components/Home.vue';
console.log(this.$router,pus); //the undefined type
export default {
name: 'app',
data() {
return {
isLogin: true
}
},
components: {
'app-headnav': HeadNavBar,
'app-sidebarnav': SideBarNav,
'app-home': Home
}
}
</script>
But when I moved it into the methods section, I achieved the desired outcome:
export default {
name: 'app',
data() {
return {
isLogin: true
}
},
components: {
'app-headnav': HeadNavBar,
'app-sidebarnav': SideBarNav,
'app-home': Home
},methods: {
myFunc: function() {
console.log(this.$router,pus); // this gave result
}
}
}