V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
onice

vue 等现代前端框架,是怎么实现路由功能的呢?

  •  
  •   onice · Sep 23, 2022 · 1490 views
    This topic created in 1324 days ago, the information mentioned may be changed or developed.
    各位 V 站的前端大佬好。请教个问题:

    一些使用 vue 开发的站点,在定义路由的时候,都会在路由的最后面,就是路由数组的外面,定义一个路径匹配为星号的路由,该路由跳转到首页。

    这样的效果是:当用户请求一个不存在的路由,页面会自动跳转到首页。

    但是有个问题我很好奇,这个路由的功能是怎么实现的呢?

    比如用户访问: https://xxx.com/test ,默认浏览器会请求 test 目录下的 index.html 。

    按照传统的前端开发,要实现页面跳转,应该在 test 目录下写一个 index.html ,,index.html 里写入 js 的跳转代码。

    但是在 Vue 中,test 这个目录并不存在。页面也能自动跳转。

    Java 的 SpringMVC 也有类似功能,是用 web 容器的的拦截器实现的。

    但我发现 vue 打包后的文件,就一个 html ,难道浏览器也有拦截器的功能吗?

    这是为啥呢?

    谢谢大家。
    Supplement 1  ·  Sep 23, 2022
    感谢 V 友的解答。原来是 web 中间件的配置项在起作用。

    我在 vue 的官网也找到答案了,参考链接为: https://router.vuejs.org/guide/essentials/history-mode.html#nginx
    noe132
        1
    noe132  
       Sep 23, 2022   ❤️ 1
    nginx 提供 fallback.
    try_files $uri $uri/ /index.html =404;
    dreasky
        2
    dreasky  
       Sep 23, 2022   ❤️ 1
    那时 web 服务器找不到页面自动跳转 index.html 的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2895 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 12:40 · PVG 20:40 · LAX 05:40 · JFK 08:40
    ♥ Do have faith in what you're doing.