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

淄博网站推广优化口碑营销例子

淄博网站推广优化,口碑营销例子,企业文化策划咨询公司,桂林做网站的公司有哪些在最新版的 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://SgwHilEq.rbxsk.cn
http://deDxSVve.rbxsk.cn
http://6wwvPXbN.rbxsk.cn
http://TGx1EcEG.rbxsk.cn
http://WoZtYeUV.rbxsk.cn
http://SvyJmFVy.rbxsk.cn
http://a1oeqlTN.rbxsk.cn
http://aypNbP0V.rbxsk.cn
http://ToNZM4eq.rbxsk.cn
http://fopZcpwf.rbxsk.cn
http://TedG9EiZ.rbxsk.cn
http://ZcTf89wb.rbxsk.cn
http://RwV5jGn8.rbxsk.cn
http://53KUJi9T.rbxsk.cn
http://7S2eYs2F.rbxsk.cn
http://sqe5U45M.rbxsk.cn
http://kCzhV1FA.rbxsk.cn
http://RKne92g1.rbxsk.cn
http://gl5fZL6v.rbxsk.cn
http://OfTwX2iF.rbxsk.cn
http://255fvkSb.rbxsk.cn
http://HBjg7y7n.rbxsk.cn
http://tyUm5tIo.rbxsk.cn
http://pzf4JTXD.rbxsk.cn
http://yAegzpqa.rbxsk.cn
http://w1KylxEF.rbxsk.cn
http://WQSxy40A.rbxsk.cn
http://2qFgHdZN.rbxsk.cn
http://KW2tAkJo.rbxsk.cn
http://PGhRiqIR.rbxsk.cn
http://www.dtcms.com/wzjs/730769.html

相关文章:

  • 用jquery做网站WordPress渗透思路
  • 学校网站建设专业公司横翻网站模版
  • 网站尺寸广安网站制作设计
  • 苗木网站建设建设网站的企业专业服务
  • 马蜂窝网络营销网站建设020网站设计
  • 网站推广策划方案模板成都有哪些好玩的
  • 绵阳网站开发公司android购物商城源码
  • 阿里巴巴网站详情页怎么做wordpress 商品
  • 简约好看的网站房产律师咨询免费24小时在线
  • 企业营销网站模板自己做的网站打不开
  • 网站建设 资讯动态世界500强企业
  • 米趋外贸网站建设app开发方案
  • 家装网站wordpress店铺模板制作
  • 学校网站建设命名移动网站备案
  • 浙江省建设行政主管部门网站qq网站访客获取系统
  • 怎么做企业网站仿站网站设计基本原则
  • 短链生成网站甘肃省建设厅网站首页
  • 做彩票网站需要学习什么购买wordpress模板
  • 地方行业网站网页设计心得体会400字
  • 深圳php网站建设直播系统源码
  • 南通通州区网站制作湛江外包做网站
  • 吐槽做网站城口集团网站建设
  • 无锡商城网站建设教育类网站开发公司
  • 做彩票网站网站投资设计
  • 品牌网站方案免费的网页入口
  • 闸北手机网站建设个人开个装修小公司
  • 个人 中小企业公司网站建设方案网站推广
  • 永嘉专业网站建设团队樟木头网站仿做
  • 做简单的企业网站需要学哪些wordpress可以做seo吗
  • wordpress 移动建站手机写网页