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


相关推荐

  • 基于opencv在摄像头ubuntu根据视频获取

    基于opencv在摄像头ubuntu根据视频获取

    2021年12月31日
    42
  • js中join方法

    js中join方法js中的join方法join方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。大白话:join方法可以用符不同的分隔符来构建这个字串。join方法值接受一个参数,即用作分隔符的字符串,然后返回所有数组项的字符串。vararr=["red","yellow","blue"];vararray=[];下面开始调用join方法1a…

    2022年4月29日
    79
  • java引用变量和对象(java变量和对象)

    Java对象及其引用先搞清楚什么是堆,什么是栈。Java开辟了两类存储区域,对比二者的特点存储区域 存储内容 优点 缺点 回收 栈 基本类型的变量和对象的引用变量 存取速度比堆要快,仅次于寄存器,栈数据可以共享 存在栈中的数据大小与生存期必须是确定的,缺乏灵活性。栈中主要存放一些基本类型的变量 当超过变量的作用域后,Java会自动释放…

    2022年4月13日
    70
  • 在GridView中增加LinkButton,出现错误:EnableEventValidation=”false”

    在GridView中增加LinkButton,出现错误:EnableEventValidation=”false”错误信息:Invalidpostbackorcallbackargument.Eventvalidationisenabledusing<pagesenableEventValidation=”true”/>inconfigurationor<%@PageEnableEventValidation=”true”%>inapag…

    2022年7月14日
    13
  • ODBC与JDBC比較

    ODBC与JDBC比較

    2022年1月28日
    440
  • hackbar 的简单使用

    hackbar 的简单使用https://www.cnblogs.com/wayne-tao/p/11027650.html前言:hackbar是web渗透时的经典工具,但是当我开始学习网络安全的时候,发现hackbar已经开始收费了。本篇抛砖引玉介绍几个使用方法,针对火狐浏览器的。建议与https://www.cnblogs.com/wayne-tao/tag/DVWA/一起学习。1.安装:一、Maxh…

    2022年6月9日
    144

发表回复

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

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