当前位置: 首页 > Web前端 > vue.js

vue路由模式下hash和history的区别

时间:2023-03-31 16:50:15 vue.js

相同点:1、当url改变时,页面不会重新加载;2.两者都受到页面来回导航的影响;不同点:1.URL链接显示不同,hash中会有“#”,历史中没有“#”;2、如果后台没有对应的配置,历史页面再次刷新会报404;hash不会出现404错误注意:给出警告,因为这样做之后,你的服务器将不再返回404错误页面,因为所有路径都会返回index.html文件。为了避免这种情况,你应该覆盖你的Vue应用程序中的所有路由案例,然后给出一个404页面。constrouter=newVueRouter({mode:'history',routes:[{path:'*',component:NotFoundComponent}]})history页面刷新出现"404"解决ApacheRewriteEngineOnRewriteBase/RewriteRule^index\.html$-[L]RewriteCond%{REQUEST_FILENAME}!-fRewriteCond%{REQUEST_FILENAME}!-dRewriteRule。/index.html[L]nginxlocation/{try_files$uri$uri//index.html;}原生Node.jsconsthttp=require('http')constfs=require('fs')consthttpPort=80http.createServer((req,res)=>{fs.readFile('index.htm','utf-8',(err,content)=>{if(err){console.log('我们无法打开"index.htm"文件。')}res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'})res.end(content)})}).listen(httpPort,()=>{console.log('服务器侦听:http://localhost:%s',httpPort)})