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

什么是单页面应用开发工具_单页面和多页面的区别及优缺点单页面应用开发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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 程序员star法则简历_程序员的标配

    程序员star法则简历_程序员的标配hhh程序员的表达能力一直被诟病,尤其面试讲述自己的项目的时候下面的star原则能够帮助你:所谓STAR原则,即Situation(情景)、Task(任务)、Action(行动)和Result(结果)四个英文单词的首字母组合。STAR原则是结构化面试当中非常重要的一个理论。S指的是situation,中文含义是情景,也就是在面谈中我们要求应聘者描述他在所从事岗位期间曾经做过的某件重要的且可以当作

    2022年9月13日
    3
  • 一、Linux下的SVN服务器搭建

    一、Linux下的SVN服务器搭建这里自己做个总结。环境:contos7,百度云服务下载svn服务器,必须是联网情况下。yum-yinstallsubversion查看下载后的信息,安装位置及详细信息。rpm-qlsubversion3.创建版本库目录,可以再chenjy目录上放置多个项目,不必为每个项目创建一个版本库。下面是我的版本库mkdir/opt/svn/svnrepos/ch…

    2022年7月19日
    15
  • android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序

    在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity,结果总是存在好几个同样的activity,就算要返回的activity正在前台,点击消息后也会重新打开一个一样的activity,返回好几次才能退出,而不能像qq之类的点击通知栏消息回到之前存在的activity,如果存在就不再新建一个activity说的有点绕,如果是遇到此类问题的肯定能懂,没遇到过

    2022年3月10日
    40
  • sql存储过程简单例题_sql存储过程实例详解

    sql存储过程简单例题_sql存储过程实例详解1、创建存储过程P1,查询每个学生的修课门数,要求列出学生学号、姓名及修课门数。createprocP1asselectStudent.StudentID,StudentName,count(CourseID)选修门数fromStudentjoinGradeonGrade.StudentID=Student.StudentIDgroupbyStudent.StudentID,StudentNamego2、创建存储过程P2,查询学生的学号、姓名、课程名、成绩

    2022年8月30日
    3
  • kindeditor自定义上传文件的路径[通俗易懂]

    kindeditor自定义上传文件的路径[通俗易懂]先上一张图这个项目是tp5.0做的,网站定义入口文件在public下,所以根目录下就是hook,static,upload三个文件夹。找到upload_json.php修改文件保存路径和保存目录就ok了;不骗你,再来张。…

    2022年9月13日
    0
  • 初中数学课程与信息技术的整合[通俗易懂]

    初中数学课程与信息技术的整合[通俗易懂]2.1基本工具介绍 22.1.1滑动的梯子上的猫 22.1.2智能画笔挥洒自如 72.1.3选了再做谋而后动 92.1.4公式输入即打即现 102.1.5动态测量功能多多 152.2文本命令应有尽有 182.2.1点可不简单 182.2.2直线面面观 222.2.3圆和圆弧很重要 232.2.4圆锥曲线条件多 242.2.5函数曲线最有用 252.2.6图形变换功能强 2…

    2022年5月12日
    37

发表回复

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

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