【问题标题】:Vue-router routes to the wrong page, the link has to add a '#' before the pathVue-router路由到错误的页面,链接必须在路径前添加'#'
【发布时间】:2021-10-28 23:40:17
【问题描述】:

这是我的主页:

这是我的博客页面:

我用的是vue3,代码坏了:

import { createRouter, createWebHashHistory } from 'vue-router';
import Home from '../views/Home.vue';
import Blogs from '../views/Blogs.vue';


const routes = [
    { path: '/', component: Home},
    { path: '/home', name: 'Home', component: Home },
    { path: '/blogs', name: 'Blogs', component: Blogs },

];

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

export default router;

所以,如果链接是'http://localhost:3000/blogs/',它应该是我的博客页面,但结果却是主页。

链接必须是 'http://localhost:3000/#/blogs',所以它可以路由到博客页面。

我不明白为什么我必须在实际路径之前写一个'/#/'!

【问题讨论】:

    标签: javascript vue.js vue-router vuejs3 vue-router4


    【解决方案1】:

    您应该使用createWebHistory 而不是createWebHashHistory。更多详情here.

    【讨论】:

      【解决方案2】:

      你必须使用'/#/',因为你使用的是createWebHashHistory(),如果没有使用createWebHashHistory()的具体原因,你应该使用createWebHistory(),因为这是推荐的方式。

      更多信息在这里: https://next.router.vuejs.org/guide/essentials/history-mode.html

      【讨论】:

        猜你喜欢
        • 2021-11-22
        • 1970-01-01
        • 2020-06-18
        • 2018-01-03
        • 1970-01-01
        • 2018-04-20
        • 2020-10-02
        • 2019-11-28
        • 1970-01-01
        相关资源
        最近更新 更多