我刚刚初始化了一个 Vue 应用程序并添加了一些新路由。但他们根本没有工作。
我的路由器/索引.js
import Vue from 'vue'
import Router from 'vue-router'
import QuestionList from '@/components/pages/QuestionList'
import SessionList from '@/components/pages/SessionList'
Vue.use(Router)
const router = new Router({
routes: [
{
path: '/questions',
name: 'QuestionList',
component: QuestionList
},
{
path: '/sessions',
name: 'SessionList',
component: SessionList
},
]
})
export default router
我的组件:
<template>
<h1>test</h1>
</template>
<script>
export default {
}
</script>
<style>
</style>
当我转到本地主机:8000/问题或本地主机:8000/会话时,没有任何反应。 + 我没有通过 Vue 开发工具看到这些组件。怎么了?
如official docs
中所述,您应该添加哈希,例如localhost:8000/#/sessions
:
vue-router 的默认模式是哈希模式 - 它使用 URL 哈希来模拟完整的 URL,以便在 URL 更改时不会重新加载页面。
为了摆脱哈希,我们可以使用路由器的历史模式,该模式利用 history.pushState API 实现 URL 导航,而无需重新加载页面:
const router = new VueRouter({
mode: 'history',
routes: [...]
})