路由使用history模式配置

路由使用history模式配置,第1张

域名例如:www.hh.com/test/

1、const router = new VueRouter({

  mode: "history",

  base: test,

})

 2、vue.config中

 publicPath: test

3、nginx:

location / {

            alias  /root_path/

            index index.html index.htm

            try_files $uri $uri/ @router

                }

        location @router{

                rewrite ^(.+)$ test/index.html last

        }

当我们使用Vue开发时,一般会用到vue-router来作为前端路由,实现单页应用。vue-router提供了两种模式模拟一个完整的 URL:hash模式和history模式。

hash模式:使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。

//查看id为1的文章

http://localhost:8888/#/view/1

history模式:URL 就像正常的 url,比较好看。

//查看id为1的文章

http://localhost:8888/view/1

vue-router默认是使用hash模式的,不需要额外的配置,如果我们想使用history模式该如何配置呢?

1 前端:vue-router的mode: ‘history’

constrouter =newVueRouter({

mode:'history',

routes: [ ... ]

})

2 后端:我是使用SpringBoot,要在服务端增加一个覆盖所有情况的候选资源:如果 URL 匹配不到任何静态资源,则应该返回同一个 index.html 页面,这个页面就是你 app 依赖的页面。 详细看vue-router的官方文档 。

SpringBoot默认匹配不到URL时,会返回一个默认的页面,所以可以配置一下默认的配置Bean。

@SpringBootApplication

public class BlogApiApplication {

public static void main (String[] args){

SpringApplication app = new SpringApplication(BlogApiApplication.class)

        app.run(args)

    }

@Bean

public EmbeddedServletContainerCustomizer containerCustomizer (){

return new EmbeddedServletContainerCustomizer() {

@Override

public void customize (ConfigurableEmbeddedServletContainer container){

ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND,"/index.html")

                container.addErrorPages(error404Page)

            }

        }

    }

}

这样就可以使用history模式了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7878835.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存