为什么 vue-router 不起作用?我的项目在 GitHub

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

我曾尝试通过 url 访问其他页面,但这些页面从未加载

这是我的 main.js 文件:

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')

有谁知道为什么 vue-router 不工作?

我用 Vue CLI 创建了这个项目,然后安装了 npm install vue-router,基本上我只是在 ./components/users 中添加了一个新组件,还修改了 main.js 文件,这就是全部.

我上传了我的项目到GitHub:https://github.com/alex-developer-18x/vueapp

我 运行 你的代码,问题是因为你没有使用“router-view”。转到您的 App.vue 并添加“router-view”组件。

示例(根据您的代码编辑):

<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>