2
votes

I am trying to work on vue-router. But, I am stuck with a problem and I cannot find the solution after viewing many articles and docs.

I have already read couple of articles on how to use vue-router but they are not working.

This is my code. I have made three components Home Login HelloWorld

I have made three routes.

Here is the code

main.js:

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router';
// import Home from './components/Home.vue';
import HelloWorld from './components/HelloWorld.vue';
import Login from './components/Login.vue';


Vue.use(VueRouter)

Vue.config.productionTip = false


const router = new VueRouter({
  mode: 'history',
  base: __dirname,
  routes: [
    { path: '/hello', component: HelloWorld },
    { path: '/login', component: Login },
    { path: '/', component: App }
  ]
});


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

App.vue:

<template>
  <div id="app">
    <HelloWorld/>
    <Login />
  </div>
</template>

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

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

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Login.vue:

<template>
  <div>
    <h1>Login </h1>    
  </div>
</template>

<script>
export default {
  name: 'Login',

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

HelloWorld.vue:

<template>
  <div>
    <h1>Hello World </h1>    
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

package.json

  "name": "test-vue-router",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "core-js": "^2.6.5",
    "vue": "^2.6.10",
    "vue-router": "^3.0.6"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.8.0",
    "@vue/cli-plugin-eslint": "^3.8.0",
    "@vue/cli-service": "^3.8.0",
    "babel-eslint": "^10.0.1",
    "eslint": "^5.16.0",
    "eslint-plugin-vue": "^5.0.0",
    "vue-template-compiler": "^2.6.10"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "rules": {},
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions"
  ]
}

Neither of the routes are working. Everytime, I press enter by changing URL. the base route (i.e /) always shows up.

And it shows App.vue compoenent.

Thanks!

2
Try putting your current third route (/) as the first one.Tim VN

2 Answers

3
votes

Your template inside app.vue is incorrect, Try this:

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

Note that the <router-view/> tag is the place where you pages/components will be rendered.

Also the base route / on your routes array is incorrect, the App component is the root component and it will be holding other pages/components as its childs so App component cannot be one of the routes.

5
votes

It seem like your just missing your <router-view/> tag on App.vue. <router-view/> will render your components depending on the url so you don't need to import the components yourself.

Try this out:

<template>
  <div id="app">
    <router-view/>
  </div>
</template>