在过去,路由主要是 Server 负责,根据不同的路由去决定要渲染的页面,回传整个 HTML 回来,现在较多的架构是前後端分离
,前端负责页面呈现资料;後端负责提供资料
只要 API 格式定义好,前端可以随意选择任何框架或无框架开发
一种对照表的概念,根据不同的url地址来显示不同的页面或内容的功能
如果每次换页都需要刷新页面,会影响使用者体验,於是发展出 由前端管理路由
原本後端负责的路由改由前端处理,从头到尾只有一个 index.html
,切换页面时,不是整个页面切换,而是发送 Request 取得 Response,将资料更新
在这样只有一个 index.html 的情况下,浏览器大致有两种路由模式可以使用:
https://www.example.com/#yoyoyo
hashchange
事件pushState()
和 replaceState()
(History API)onpopstate
事件监听可以使用 history 物件中的 back()
forward()
go()
在历史纪录中移动,HTML5 增加的 history.pushState
history.replaceState()
可以异动历史纪录
ex:
history.pushState(state [,title][,url]); // 历史记录中增加一条记录
history.replaceState(state [,title][,url]); // 修改目前的历史纪录
vue add router
// 会询问是否使用 history mode
产生以下档案:
About.vue
和 Home.vue
,代表页面ex:
// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
}
]
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
})
export default router
<!-- App.vue -->
<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
</template>
使用 router-link
标签,to
属性後面接要造访的页面,router-view
放置处代表要渲染元件的位置
ex:
// App.vue
<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
<router-link to="/profile">Profile</router-link>
</div>
<router-view/>
</template>
未完待续...
How to Use Vue Router: A Complete Tutorial
浅谈新手在学习 SPA 时的常见问题:以 Router 为例
每日一句:
批评你的人,是你生命中的贵人
<<: [Day 21] 妈! Keras 和 TensorFlow 在乱存模型啦! ( TFLite 轻量模型)
触碰开灯 ( 类比讯号 ) 教学原文参考:触碰开灯 ( 类比讯号 ) 这篇文章会介绍如何使用「序列写...
跳着写呀XD"~ 简单来说,就是维运罗(个人理解啦,也可能想错了)。 ▉A.12.1 运作...
1.首先我们需要下载一个编辑器来写程序。 https://www.sublimetext.com/ ...
iOS APP iOS Test-Driven Development by Tutorials f...
接下来是一题迄今为止,作业中最复杂的一题,虽然不会太难,但是要把逻辑整理清楚,而且给予自己足够的信心...