alex-developer-18x
alex-developer-18x

Reputation: 15

Why vue-router does not work? My project is in GitHub

I have tried to access to others pages via url, but those pages never loads

This is my main.js file:

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

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

import Users from './components/Users.vue'
const Home = { template: "<p>home page</p>" };
const Index = { template: "<p>index page</p>" };
const About = { template: "<p>about page</p>" };

const routes = [
  { path: "/users", name: "users", component: Users },
  { path: "/home", name: "home", component: Home },
  { path: "/index", name: "index", component: Index },
  { path: "/about", name: "about", component: About },
];

var router = new VueRouter({
  routes: routes,
  mode: "history",
});

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

Does anyone know why vue-router not work?

I created this project with Vue CLI, and then I intalled npm install vue-router, basically i just added a new component in ./components/users and also modified the main.js file, and that's all.

I uploaded my project to GitHub: https://github.com/alex-developer-18x/vueapp

Upvotes: 1

Views: 2404

Answers (1)

BakaDesu
BakaDesu

Reputation: 172

I ran your code and the problem is because you are not using "router-view". Go to your App.vue and add "router-view" component.

Example (Edited from your code):

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <!-- You should add this instead of <HelloWorld/> -->
    <router-view></router-view>
  </div>
</template>

<script>
// Remove the Hello World import here
export default {
  name: 'App',
  components: {
    // And remove the Hello World component here
  }
}
</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>

Upvotes: 2

Related Questions