分析下列代码
javascript
const router = new VueRouter({
mode:'history',
routes
})
1.const router = new VueRouter({ ... })
用来创建一个 Vue Router 实例,用于管理 Vue.js 应用的路由。
2.mode: 'history':
作用: 启用 HTML5 History 模式,去除 URL 中的 # 符号(默认是 Hash 模式,如 http://example.com/#/home)。
效果: URL 更简洁(如 http://example.com/home)。
**注意:**需服务器配置(如 Nginx、Apache)支持,确保直接访问子路径时返回 index.html,否则会出现 404 错误。
3.routes:
作用:定义路由规则数组,每个路由映射到对应的 Vue 组件。
javascript
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
];