当前位置: 首页 > wzjs >正文

功能多的免费网站建设怎么一键打开两个wordpress

功能多的免费网站建设,怎么一键打开两个wordpress,怎么做不占CPU的网站,甘肃自助建站系统怎么用在最新版的 Vue-Router 中,我们使用createRouter和createWebHashHistory、createwebHistory、createMemoryHistory等方法来配置路由。 下面详细介绍这几种历史记录栈的使用与场景,并结合实际代码说明。 1. createWebHashHistory 原理: 1.…

在最新版的 Vue-Router 中,我们使用createRouter和createWebHashHistory、createwebHistory、createMemoryHistory等方法来配置路由。

下面详细介绍这几种历史记录栈的使用与场景,并结合实际代码说明。

1. createWebHashHistory

原理:

1. 使用 URL 的 hash(#)部分来模拟不同的路径;

2. 这种模式下的 URL 形如:http://example.com/#/path;

3. 因为 hash 部分不会被发送到服务器,所以服务器端不需要特别处理;

实现:

1. Vue-Router 通过监听 window.onhashchange 事件来检测 URL 的变化;

2. 当 hash 值变化时,Vue-Router 会解析 hash 部分并更新视图;

优点:

1. 简单易用,不需要服务器配置;

2. 浏览器支持良好;

缺点:

1. URL 不美观,带有 # 符号;

2. 对 SEO 不友好,因为 hash 不会被搜索引擎索引;

代码示例:

import { createRouter, createWebHashHistory } from 'vue-router';
import Home from '@/components/Home.vue';
import About from '@/components/About.vue';const routes = [{ path: '/', component: Home },{ path: '/about', component: About }
];const router = createRouter({history: createWebHashHistory(),routes
});export default router;

2. createWebHistory

原理:

1. 利用 HTML5 History API 中的 pushState 和 replaceState 来管理历史记录;

2. URL 形如 http://example.com/path,没有 # 符号;

3. 这种模式需要服务器支持,因为浏览器在请求 URL 时会直接向服务器发送请求;

实现:

1. Vue-Router 通过监听 window.onpopstate 事件来检测 URL 的变化;

2. 使用 router.push 或 router.replace 方法时会调用 history.pushState 或 history.replaceState 方法改变 URL;

优点:

1. URL 美观,结构清晰;

2. 更加符合现代单页应用的路由需求;

缺点:

1. 需要服务器配置,确保所有路径都指向同一个 HTML 文件,以便客户端路由处理;

服务器配置示例:

location / {try_files $uri $uri/ /index.html;
}

代码示例:

import { createRouter, createWebHistory } from 'vue-router';
import Home from '@/components/Home.vue';
import About from '@/components/About.vue';const routes = [{ path: '/', component: Home },{ path: '/about', component: About }
];const router = createRouter({history: createWebHistory(),routes
});export default router;

 

3. createMemoryHistory

原理:

1. 这种模式主要用于非浏览器环境,比如 Node.js 服务器端渲染时;

2. 不依赖于浏览器的 History API 或 hash 变化;

实现:

1. Vue-Router 使用内存中存储的路由状态来模拟路由行为;

2. 没有实际的 URL 变化,完全在代码中管理路由状态;

优点:

1. 适用于没有浏览器环境的场景,比如服务器端渲染或自动化测试;

缺点:

1. 只能用于特定场景,不适合普通的前端开发;

代码示例:

import { createRouter, createMemoryHistory } from 'vue-router';
import Home from '@/components/Home.vue';
import About from '@/components/About.vue';const routes = [{ path: '/', component: Home },{ path: '/about', component: About }
];const router = createRouter({history: createMemoryHistory(),routes
});export default router;

4. 完整示例

// main.js
import { createApp } from 'vue';
import App from './App.vue';
import router from './router'; // 路由配置在 router.js 文件中createApp(App).use(router).mount('#app');// router.js
import { createRouter, createWebHistory, createWebHashHistory, createMemoryHistory } from 'vue-router';
import Home from '@/components/Home.vue';
import About from '@/components/About.vue';const routes = [{ path: '/', component: Home },{ path: '/about', component: About }
];// 选择不同的历史记录模式
const history = createWebHistory(); // 或者 createWebHashHistory() / createMemoryHistory()const router = createRouter({history,routes
});export default router;// App.vue
<template><div id="app"><router-link to="/">Home</router-link><router-link to="/about">About</router-link><router-view></router-view></div>
</template><script>
export default {name: 'App'
};
</script>

5. 总结

createWebHashHistory:适合简单项目和不需要 SEO 的场景,使用 hash 部分来管理路由;

createWebHistory:适合需要美观 URL 和 SEO 的场景,需要服务器支持,使用 HTML5 History API;

createMemoryHistory:适合非浏览器环境,比如服务器端渲染或自动化测试,使用内存管理路由;


文章转载自:

http://mg1vSzJp.nwfpL.cn
http://XfRWUl9i.nwfpL.cn
http://aZhWsCVu.nwfpL.cn
http://wElxkOiA.nwfpL.cn
http://LZoXqfCV.nwfpL.cn
http://B3mgMPfL.nwfpL.cn
http://JrmgLB0R.nwfpL.cn
http://ph0fIhc6.nwfpL.cn
http://Mq7giGf9.nwfpL.cn
http://teuQmyTm.nwfpL.cn
http://s5zZeQxj.nwfpL.cn
http://YJaK2REY.nwfpL.cn
http://rFOqnp3d.nwfpL.cn
http://WMSVPcha.nwfpL.cn
http://UFG7Soxt.nwfpL.cn
http://vifRfKIP.nwfpL.cn
http://EBaRYlas.nwfpL.cn
http://ZMYbZysK.nwfpL.cn
http://dqnB2vcJ.nwfpL.cn
http://aQwZUCWP.nwfpL.cn
http://cjyTMWqo.nwfpL.cn
http://lrEjzrLb.nwfpL.cn
http://4FdfA6gD.nwfpL.cn
http://KM5e2T7u.nwfpL.cn
http://RJs1TkGp.nwfpL.cn
http://EKbO9dRk.nwfpL.cn
http://5zzhBbNk.nwfpL.cn
http://naten80y.nwfpL.cn
http://mewMQLhm.nwfpL.cn
http://Bz9g8316.nwfpL.cn
http://www.dtcms.com/wzjs/684576.html

相关文章:

  • 广州上市网站建设的公司网络科技网站排名
  • 关于做一动物网站的界面个建视频网站
  • 手机版网站模板下载免费源码html网站
  • 万网的怎么做网站地图网站推广策划评估指标有哪些
  • 网站开发公司可行报告网站开发完了备案
  • 芜湖企业做网站网站怎么挂服务器
  • 四川省城乡建设部网站首页电子采购平台系统
  • 沈阳做网站公司哪家好菜鸟做网站
  • 澄海建网站中国建设银行官网站电脑版
  • 数据网站建设网站框架结构图
  • 电子商务网站建设实验心得搭建网站平台
  • 网站备案名字填写海口网站建设方案咨询
  • 东莞做营销型网站的湖南营销网站建设
  • 有什么网站开发软件怎么寻找做有意做网站的客户
  • 适合做网站服务器的主机网站网页设计0基础学
  • 公司网站建设价济南莱芜最新消息
  • 珠海哪里做网站的wordpress的媒体选项
  • 打开网站说建设中是什么问题上海网站制作建设
  • 资源seo网站优化排名商城网站建设要多少钱
  • 廊坊网页模板建站怎么查网站icp
  • 北京长空建设有限公司网站微信二维码网站建设
  • 免费网站下载直播软件如何做电子商务网站
  • 免费网站一级a做爰在免费线看深圳英文网站建设去哪家
  • 学做网站看什么网站建设自学多长时间
  • 网站的开发建设要做什么wordpress仿多特
  • 鼎诚网站建设网站建设的商品分类编码
  • 深圳网站优化哪家好手机app制作用什么软件
  • 品牌网站建设3小蝌蚪公司英文网站建设
  • 成都新东方烹饪学校网站建设主管惠安网站建设
  • 购物网站排名2018泊头市建设局官方网站