路由器的两种工作模式:hash模式和history模式

路由器的两种工作模式:hash模式和history模式文章目录hash模式的路由器history模式的路由器history模式下的404问题及其解决方法打包前端项目express构建小型服务器前端打包文件部署至服务器解决404报错问题url中,#后面的字符串就是hash值。hash是和浏览器对话的,和服务器没有关系,hash值不会作为url的一部分发送给服务器。路由器(vue-router)有两种工作模式:hash模式和history模式,默认是hash模式。hash模式的路由器路由器默认的工作模式是hash模式。因此我们看到的是http://lo

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

url中,#后面的字符串就是hash值。
hash是和浏览器对话的,和服务器没有关系,hash值不会作为url的一部分发送给服务器。

路由器(vue-router)有两种工作模式:hash模式和history模式,默认是hash模式。

hash模式的路由器

路由器默认的工作模式是hash模式。因此我们看到的是
http://localhost:8080/#/
http://localhost:8080/#/about
http://localhost:8080/#/home

  • 普通组件components/Banner.vue
<template>
    <div class="col-xs-offset-2 col-xs-8">
        <div class="page-header">
            <h2>Vue Router Demo</h2>
        </div>
    </div>
</template>

<script> export default { 
     name:"Banner" } </script>

<style> </style>

Jetbrains全家桶1年46,售后保障稳定

  • 路由组件pages/Home.vue
<template>
  <div>
    <h2>Home组件内容</h2>
  </div>
</template>

<script> export default { 
     name:'Home' } </script>

<style> </style>
  • 路由组件pages/About.vue
<template>
  <h2>我是About的内容</h2>
</template>

<script> export default { 
     name:'About' } </script>

<style> </style>
  • 路由器router/index.js
import VueRouter from "vue-router";
import Home from "../pages/Home";
import About from "../pages/About";

const router = new VueRouter({ 
   
    routes:[
        { 
      
            path:"/about",
            component:About
        },
        { 
   
            path:"/home",
            component:Home
        }
    ]
})

export default router;
  • App.vue
<template>
  <div>
    <div class="row">
      <Banner/>
    </div>
    <div class="row">
      <div class="col-xs-2 col-xs-offset-2">
        <div class="list-group">
          <router-link to="/about" active-class="active" class="list-group-item">About</router-link>
          <router-link to="/home" active-class="active" class="list-group-item">Home</router-link>
        </div>
      </div>
      <div class="col-xs-6">
        <div class="panel">
          <div class="panel-body">
            <keep-alive>
              <router-view></router-view>
            </keep-alive>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script> import Banner from "./components/Banner.vue"; export default { 
     name: 'App', components:{ 
     Banner } } </script>
  • 入口组件main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from "vue-router";

import router from "./router/index";

Vue.config.productionTip = false;

Vue.use(VueRouter);

new Vue({ 
   
  render: h => h(App),
  router:router
}).$mount('#app')
  • 启动应用,测试效果。
    在这里插入图片描述

history模式的路由器

路由器工作模式为history模式时,我们看到的是
http://localhost:8080/
http://localhost:8080/about
http://localhost:8080/home

  • 修改路由器router/index.js
    要将路由器的工作模式从默认的hash模式修改为history模式,只需要修改router/index.js,将mode设置为history
import VueRouter from "vue-router";
import Home from "../pages/Home";
import About from "../pages/About";

const router = new VueRouter({ 
   
    mode:"history",
    routes:[
        { 
      
            path:"/about",
            component:About
        },
        { 
   
            path:"/home",
            component:Home
        }
    ]
})

export default router;

在这里插入图片描述

  • 重启应用,测试效果。
    在这里插入图片描述

history模式下的404问题及其解决方法

打包前端项目

history模式下,npm run build打包项目,得到dist目录。
在这里插入图片描述

express构建小型服务器

  1. 初始化项目,npm init -y
  2. 安装express,npm install –save express
  3. 项目根目录下新建文件server.js
const express = require("express");

const app = express();

app.use(express.static(__dirname+"/static"));

app.get("/person",(request,response) => { 
   
    response.send({ 
   
        name:"Nicholas",
        gender:'Male'
    })
})

app.listen(3000,(err) =>{ 
   
    if(!err) console.log("服务器启动了!");
})
  1. 项目根目录下新建子目录static,static下新建文件index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>index</title>
</head>
<body>
    <h2>hello</h2>
</body>
</html>
  1. 启动服务器,node server
D:\vue\vue_server>node server
服务器启动了!
  1. 打开浏览器,访问localhost:3000。
    在这里插入图片描述

前端打包文件部署至服务器

所谓“前端打包文件部署至服务器”,就是将前面得到的dist整个目录(及其子目录中的所有文件)放入服务器的static目录下。放好后,访问localhost:3000。
在这里插入图片描述
可以看到,history模式下的dist部署到服务器后,路由切换访问/home或/about时,没有问题。但在/home时刷新页面,显示无法找到,报404。
hash模式下的dist部署到服务器后,就不会出现以上问题。

解决404报错问题

刷新时报404的问题,需要后端程序员的帮助。本篇使用node示范下。

  • 安装第三方包,npm install –save connect-history-api-fallback。
  • 使用第三方包,修改server.js,如下。
const express = require("express");

const app = express();

const history = require("connect-history-api-fallback");
app.use(history());

app.use(express.static(__dirname+"/static"));

app.get("/person",(request,response) => { 
   
    response.send({ 
   
        name:"Nicholas",
        gender:'Male'
    })
})

app.listen(3000,(err) =>{ 
   
    if(!err) console.log("服务器启动了!");
})

在这里插入图片描述

  • 重启服务器,node server。
    在这里插入图片描述
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234265.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • oracle修改用户名密码语句_oracle修改数据库用户名

    oracle修改用户名密码语句_oracle修改数据库用户名oracle修改用户名,密码首先我们打开命令行,cmd,进入oracle的后台管理界面,当然,还有很多其他的方式可以进入oracle后台管理界面,比如使用PL/SQL界面化工具,或者使用oracle自带的SQL工具。打开命令窗口,登录oracle后台管理界面:具体命令为在运行处输入cmd,在弹出的命令提示窗口输入setoracle_sid=数据库名称,输入sqlplus进入ora…

    2022年7月28日
    7
  • PathFileExists用法--使用#include

    PathFileExists用法--使用#includeBOOLPathFileExists(LPCTSTRpszPath);
            Determinesifafileexists.
    —经检测,该函数可以检测文件或目录是否存在!Remarks
    Thisfunctionteststhevalidityofthefileandpath.Itworksonlyonthelocalfilesystemoronaremotedrivethathasbeenmoun

    2022年7月12日
    29
  • C++之运算符重载

    C++预定义中的运算符的操作对象只限于基本的内置数据类型,但是对于我们自定义的类型(类)是没有办法操作的,但是大多时候我们需要对我们定义的类型进行类似的运算,这个时候就需要我们对运算符进行重新定义,赋

    2021年12月19日
    60
  • 分布式锁的应用场景和三种实现方式的区别_负载均衡策略

    分布式锁的应用场景和三种实现方式的区别_负载均衡策略多线程对同一资源的竞争,需要用到锁,例如Java自带的Synchronized、ReentrantLock。但只能用于单机系统中,如果涉及到分布式环境(多机器)的资源竞争,则需要分布式锁。分布式锁的主要作用:保证数据的正确性:比如:秒杀的时候防止商品超卖,表单重复提交,接口幂等性。避免重复处理数据:比如:调度任务在多台机器重复执行,缓存过期所有请求都去加载数据库。分布式锁的主要特性:互斥:同一时刻只能有一个线程获得锁。可重入:当一个线程获取锁后,还可以再次获取这个锁,避免死锁发生。高可用:当

    2025年10月5日
    3
  • python 进制转换[通俗易懂]

    python 进制转换[通俗易懂]文章目录前言一、Python提供的进制转换方法1.二进制转十进制2.八进制转十进制3.十六进制转十进制4.十进制转十六进制5.十进制转二进制6.十进制转八进制二、自定义进制转换1.转十进制2.十进制转七进制总结前言十进制是我们常用的数字形式,但机器使用的却是二进制,八进制,十六进制之类的,所以进制转换是基础要求,很多编程语言提供的有进制转换的方法,下面我们开始学习一、Python提供的进制转换方法1.二进制转十进制s=’1001’print(int(s,2))结果如下:in..

    2022年5月12日
    55
  • php实现工厂模式

    php实现工厂模式

    2021年11月15日
    57

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号