vue 的路由模式分为history模式和hash模式两种。hash模式会在地址栏中添加#字符串,用于分隔前端路由。 在地址栏里面输入hash模式的地址时,会进行分割。 先判断能不能在router.js文件中找到对应的配置,如果能找则是路由地址,否则会想服务器发送请求。但是history模式不包含标志位,所以都会直接向服务器发送请求。所以我们需要做配置。 这里以nginx为例。

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            # 添加这行配置 
			try_files $uri $uri/ @router;#cl
        }
		
		# 添加这段配置  
		location @router {
            rewrite ^.*$ /index.html last;
        }

        ...}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐