什么是单页面应用开发工具_单页面和多页面的区别及优缺点

什么是单页面应用开发工具_单页面和多页面的区别及优缺点单页面应用开发MPA与SPA简介MPAMPA(Multi-pageApplication)多页面应用指的就是最传统的HTML网页设计,早期的网站都是这样的设计,所之称为「网页设计」。使用MPA在使用者浏览Web时会依据点击需求切换页面,浏览器会不停的重载页面(Reload),整个操作也常感觉卡卡。如果使用这样的设计在WebApp中,使用者体验比较差,整体流畅度扣分…

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

Jetbrains全系列IDE稳定放心使用

单页面应用开发

MPA与SPA简介

MPA

MPA (Multi-page Application) 多页面应用指的就是最传统的 HTML 网页设计,早期的网站都是这样的设计,所之称为「网页设计」。使用 MPA 在使用者浏览 Web 时会依据点击需求切换页面,浏览器会不停的重载页面 (Reload),整个操作也常感觉卡卡。如果使用这样的设计在 Web App 中,使用者体验比较差,整体流畅度扣分。但进入门槛低,简单套个 jQuery 就可以完成。

SPA

SPA (Single-page Application) 顾名思义在 Web 设计上使用单一页面,利用 JavaScript 操作 Dom 的技术实现各种应用,现今在介面上算是非常受欢迎的设计,搭配 AJAX 使得整体页面反应速度相当迅速,配合上路由懒加载等手段可以达到Native应用的体验。

图解

在这里插入图片描述

在这里插入图片描述

对比

在这里插入图片描述

实现流程

在这里插入图片描述

代码实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>原生实现hash和browser两种路由模式</title>
</head>
<body>
    <div class="router_box">
        <a href="/home" class="router" replace="true">主页</a>
        <a href="/news" class="router">新闻</a>
        <a href="/team" class="router">团队</a>
        <a href="/about100" class="router">关于</a>
    </div>
    <div id="router-view"></div>

    <script>
        function Router(params){
            // 记录routes配置
            this.routes = params.routes || [];
            // 记录路由模式
            this.mode = params.mode || 'hash';

            // 初始化
            this.init = function(){
                // 绑定路由响应事件
                var that = this;
                document.querySelectorAll(".router").forEach((item,index)=>{
                    item.addEventListener("click",function(e){
                        // 阻止a标签的默认行为
                        if ( e && e.preventDefault ){
                            e.preventDefault(); 
                        }else{
                            window.event.returnValue = false;  
                        } 
                        
                        if (that.mode == 'hash'){
                            // 判断是replace方法还是push方法
                            if (this.getAttribute("replace")){
                                var i = window.location.href.indexOf('#')
                                // 通过replace方法直接替换url
                                window.location.replace(
                                    window.location.href.slice(0, i >= 0 ? i : 0) + '#' + this.getAttribute("href")
                                )
                            }else{
                                // 通过赋值追加
                                window.location.hash = this.getAttribute("href");
                            }
                        }else{
                            if (this.getAttribute("replace")){
                                window.history.replaceState({}, '', window.location.origin+this.getAttribute("href"))
                            }else{
                                window.history.pushState({}, '', window.location.origin+this.getAttribute("href"))
                            }
                            // 手动调用更新内容方法
                            that.routerChange();
                        }
                        
                    }, false);
                });
                // 监听路由改变
                if (this.mode == 'hash'){
                    window.addEventListener("hashchange",()=>{
                        this.routerChange();
                    });
                }else{
                    window.addEventListener('popstate', e => {
                        console.log(123);
                        this.routerChange();
                    })
                }
                // 第一次进入的时候更新视图
                this.routerChange();
            },
            // 路由改变监听事件
            this.routerChange = function(){
                if (this.mode == 'hash'){
                    let nowHash=window.location.hash;
                    let index=this.routes.findIndex((item,index)=>{
                        return nowHash == ('#'+item.path);
                    });

                    if(index>=0){
                        // 查找到对应路由
                        document.querySelector("#router-view").innerHTML=this.routes[index].component;
                    }else {
                        // 没找到对应路由,查找有没有*
                        let defaultIndex=this.routes.findIndex((item,index)=>{
                            return item.path=='*';
                        });
                        // 查找到*,执行重定向
                        if(defaultIndex>=0){
                            const i = window.location.href.indexOf('#')
                            window.location.replace(
                                window.location.href.slice(0, i >= 0 ? i : 0) + '#' + this.routes[defaultIndex].redirect
                            )
                        }
                    }
                }else{
                    let path = window.location.href.replace(window.location.origin, '');
                    let index=this.routes.findIndex((item,index)=>{
                        console.log('path...', path, 'item.path...', item.path);
                        return path == item.path;
                    });
                    if(index>=0){
                        // 查找到对应路由
                        document.querySelector("#router-view").innerHTML=this.routes[index].component;
                    }else {
                        // 没找到对应路由,查找有没有*
                        let defaultIndex=this.routes.findIndex((item,index)=>{
                            return item.path=='*';
                        });
                        // 查找到*,执行重定向
                        if(defaultIndex>=0){
                            window.history.replaceState({}, '', window.location.origin+this.routes[defaultIndex].redirect)
                            this.routerChange();
                        }
                    }
                }
            }
            // 调用初始化
            this.init();
        }
        new Router({
            mode: 'history',
            routes:[
                { path: '/home', component: '<h1>主页</h1><h4>新一代前端工程师:我们啥都会</h4>' },
                { path: '/news', component: '<h1>新闻</h1><h4>今天2018-11-5,上课还得穿工装</h4>' },
                { path: '/team', component: '<h1>团队</h1><h4>WEB前端工程师</h4>' },
                { path: '/about', component: '<h1>关于</h1><h4>一面而高薪就业</h4>' },
                { path:'*', redirect:'/home'}
            ]
        });
    </script>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

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


相关推荐

  • 重启MySQL服务(怎么重启mysql服务)

    重启MySQL服务(怎么重启mysql服务)一、MYSQL服务我的电脑——(右键)管理——服务与应用程序——服务——MYSQL——开启(停止、重启动)二、命令行方式Windows1.点击“开始”->“运行”(快捷键Win+R)。2.启动:输入netstopmysql3.停止:输入netstartmysql提示*RedhatLinux也支持servicecommand,启动:#servicemysqldstar…

    2022年4月18日
    255
  • [计算机网络]交换机VLAN的作用和原理

    [计算机网络]交换机VLAN的作用和原理本节将讨论什么是交换机VLAN产生的原因,以及如何起作用的

    2022年8月10日
    5
  • Spring Boot集成AD域实现统一用户认证

    1.引言由于近期需要开发基于JWTToken的统一身份认证服务项目,因此需要集成公司原有的AD域实现用户的身份认证问题,项目采用SpringBoot框架进行开发,在此将相应的集成开发步骤进行记录。1.1LDAP简介目录是一个为查询、浏览和搜索而优化的专业分布式数据库,它呈树状结构组织数据,就好象Linux/Unix系统中的文件目录一样。目录数据库和关系数据库不同,它有…

    2022年4月8日
    238
  • CTK环境搭建

    CTK环境搭建CTK环境搭建1 下载相关文件下载Cmake:https://cmake.org/下载ctk源码:https://github.com/commontk/CTK2 编译CTK2.1 cmake打开cmakelistctk源码下载完之后在当前文件夹下新建build目录。如图2.2 qt环境版本确认使用cmake开进入CTK-Master目录,选择源代码目录和Build目录。进行…

    2022年6月6日
    64
  • Linux系统终端里的root@localhost变成root@bogon的主机名改变的解决办法

    Linux系统终端里的root@localhost变成root@bogon的主机名改变的解决办法bogon 是主机名利用 hostname 可以查看当前主机名 vi etc sysconfig network 中修改 HOSTNAME 必须重新启动才能生效 Linux 主机名被修改成 bogon 问题的几种解决办法当 Linux 主机名由 myhostname 变成了 bogon 了之后 访问网络就会出现问题 重启后也没有解决 网上搜索 N 久之后 发现了如下几种解决方式 特此记录一下 1 在 linux 下添

    2025年6月8日
    0
  • Ubuntu 软件卸载[通俗易懂]

    Ubuntu 软件卸载[通俗易懂]1.卸载程序和所有配置文件。在终端中输入以下命令,卸载需要完全移除的程序:sudoapt-get–purgeremovesoftname2.只卸载程序。如果你移除程序但保留配置文件,请输入以下命令:sudoapt-getremovesoftname参考:Ubuntu16.04软件卸载-简书…

    2022年9月13日
    0

发表回复

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

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