vue3+vite使用History路由模式打包部署项目注意事项

一、Vue Router 的两种路由模式
 

哈希模式(Hash Mode)

        默认模式,URL 结构包含哈希符号 #,例如:http://example.com/#/home。利用 window.location.hash 来跟踪路由变化,浏览器会自动处理哈希的变化而不触发页面重载。
因为哈希值只影响浏览器的历史记录而不发送到服务器,所以无需后端配合即可实现前端路由。
SEO 性能较差,因为搜索引擎通常不会抓取哈希值后面的路径内容。


历史模式(History Mode)

        利用了 HTML5 的 History API,如 window.history.pushState() 和 window.onpopstate 事件。
在此模式下,URL 不包含哈希,呈现常规的路径结构,例如:http://example.com/home。
为了实现前进、后退按钮的正常工作以及防止直接访问某个路由导致404错误,需要在服务器端进行适当的配置,将所有的请求都指向应用程序的入口文件,以便 Vue Router 能够接管并解析正确的路由。
历史模式提高了用户体验,URL 易于阅读,也更利于 SEO。

二、History 路由模式打包部署项目

        在Vue 3.中使用Vue Router的history模式进行项目打包部署时,需要确保打包配置和服务器正确配置来处理路由。否则部署到Nginx会出现访问404、空白等。在项目打包配置及Nginx配置有些要注意的地方。以项目名demo为例。

1、配置Vue Router为history模式,在src/router/index.js中配置
import { createRouter, createWebHistory } from 'vue-router'
 

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes:[
     // 定义路由
  ]
})
 
export default router

备注:createWebHistory:history模式,createWebHashHistory:hash模式

2、配置 vite.config.js
import {
	fileURLToPath,
	URL
} from 'node:url'

import {
	defineConfig
} from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig(() => {
	return {
		plugins: [
			vue()
		],
		// 静态站点根路径配置;base:'/demo/';demo是项目名,解决Nginx部署时,访问缺少项目前缀的问题
		base: process.env.NODE_ENV === 'production' ? '/demo/' : '/',// vue-router中配置,组件匹配
		// publicPath: process.env.NODE_ENV === 'production' ? '/demo/' : '/',// vue.config.js文件中配置,打包后外部资源的获取
		build: {
			// 设置打包文件夹的名称
			outDir: 'demo'
		},
		resolve: {
			alias: {
				'@': fileURLToPath(new URL('./src', import.meta.url))
			}
		},
	};
});
3、配置Nginx,在conf/nginx.conf中配置
location /demo/ {
    alias  /path/html/demo/;
    index  index.html index.htm;
    try_files $uri $uri/ /demo/index.html;
}

备注:alias  /path/html/demo/; 中的 path 换成Nginx对应的部署路径

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,Vue 3.x、TypeScript和Vite都支持使用路由。您可以使用Vue Router 4.x来实现路由功能。下面是一个简单的示例,展示了如何在Vue 3.x、TypeScript和Vite使用路由: 1. 首先,您需要安装Vue Router和相关的依赖。您可以使用以下命令来安装它们: ``` npm install vue-router@4.x ``` 2. 在您的Vue 3.x项目中,创建一个router.ts文件,并在其中定义路由配置。以下是一个基本的示例: ```typescript import { createRouter, createWebHistory } from 'vue-router'; // 导入组件 import Home from './views/Home.vue'; import About from './views/About.vue'; const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ]; const router = createRouter({ history: createWebHistory(), routes }); export default router; ``` 3. 在入口文件(main.ts)中,将路由添加到Vue应用中: ```typescript import { createApp } from 'vue'; import App from './App.vue'; import router from './router'; createApp(App).use(router).mount('#app'); ``` 4. 创建两个组件,Home.vue和About.vue,分别表示首页和关于页面。您可以根据自己的需求进行修改和扩展。 5. 最后,在模板文件中使用<router-link>和<router-view>标签来实现路由导航和内容渲染。例如,在App.vue中可以这样使用: ```html <template> <div> <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> <router-view></router-view> </div> </template> ``` 通过以上步骤,您就可以在Vue 3.x、TypeScript和Vite环境下成功使用路由了。请注意,这只是一个简单的示例,您可以根据自己的需求进行路由配置和页面组件的编写。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值