路由器的两种工作模式: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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • HTML5移动开发的10大移动APP开发框架

    HTML5移动开发的10大移动APP开发框架今天给大家介绍10款有关HTML5移动开发APP开发框架,这几款框架都是比较优秀的移动Web开发框架,能够帮助开发者更加高效的开发移动Web应用。.  十款移动APP开发框架:1.jquerymobile框架2.bootstrap框架3.ionic框架4.MobileAngularUI框架5.IntelXDK框架6.AppceleratorTitanium框架7.Senc

    2022年6月21日
    26
  • Java数组的三种打印方式

    Java数组的三种打印方式1.数组的输出的三种方式一维数组:定义一个数组int[]array={1,2,3,4,5};(1)传统的for循环方式for(inti=0;i<array.length;i++){System.out.println(array[i]);}(2)foreach循环for(inta:array)System.out…

    2022年4月27日
    39
  • idea修改文字大小_为什么idea设置不了字体大小

    idea修改文字大小_为什么idea设置不了字体大小idea设置修改字体大小与样式详细步骤【备注】:不同idea版本设置方法类似,找到对应的面板设置即可第一步:点击工具栏最上方的File选项第二步:选择Setting选项第三步:选择Appearance选项,选择size设置自己喜欢的大小即可,我设置为14第四步:选择Editor选项中的font面板,同样找到size,设置对应的大小,即可设置代码主窗口的字体大小ide…

    2022年8月29日
    0
  • 京东云免费SLL正式(HTTPS)免费申请及nginx配置

    京东云免费SLL正式(HTTPS)免费申请及nginx配置京东云免费ssl证书申请及配置一、证书申请1、京东云地址:[https://www.jdcloud.com/](https://www.jdcloud.com/)2、登录京东云后可在左上角云服务中搜索:SSL3、选择搜索出的选项中的SSL数字证书,进入SSL证书管理平台;4、在左侧选择申购管理,然后点击申购证书5、在申购SSL证书界面,按下图选择对应的证书类型,即可购买;6、订单确认,立即支付即可完成证书的申请二、证书配置1、完善域名信息1、在申购管理中,找到刚才购买的证书,点击完善信息;2、在下面的输入

    2022年10月14日
    0
  • resque 遍历加载job目录下的类

    resque 遍历加载job目录下的类

    2022年2月21日
    38
  • Pytest(16)随机执行测试用例pytest-random-order[通俗易懂]

    Pytest(16)随机执行测试用例pytest-random-order[通俗易懂]前言通常我们认为每个测试用例都是相互独立的,因此需要保证测试结果不依赖于测试顺序,以不同的顺序运行测试用例,可以得到相同的结果。pytest默认运行用例的顺序是按模块和用例命名的ASCII编码

    2022年7月29日
    3

发表回复

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

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