【问题标题】:Why vue-router does not work? My project is in GitHub为什么 vue-router 不起作用?我的项目在 GitHub
【发布时间】:2020-06-04 08:00:58
【问题描述】:

我曾尝试通过 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

【问题讨论】:

    标签: vue.js vue-router router


    【解决方案1】:

    我运行了您的代码,问题是因为您没有使用“路由器视图”。转到您的 App.vue 并添加“路由器视图”组件。

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

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

    【讨论】:

    • @alex.developer.18x 欢迎 :)
    • 非常感谢,很多教程都不提!!
    猜你喜欢
    • 1970-01-01
    • 2017-04-25
    • 2020-08-10
    • 1970-01-01
    • 2017-05-31
    • 2020-12-27
    • 2018-05-28
    • 2023-03-31
    • 2021-04-04
    相关资源
    最近更新 更多