国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue中SPA單頁面應用程序詳解

2024-05-06 15:24:45
字體:
來源:轉載
供稿:網友

一、SPA的概述

SPA(single page application)單頁面應用程序,在一個完成的應用或者站點中,只有一個完整的html頁面,這個頁面有一個容器,可以把需要加載的代碼片段插入到該容器中。

SPA的工作原理:

  eg:  http://127.0.0.1/index.html#/start

①根據地址欄中url解析完整的頁面:index.html
  加載index.html

②根據地址欄中url解析#后的路由地址: start
  根據路由地址,去在當前應用的配置中 找該路由地址的配置對象去查找該路由地址 所對應的模板的頁面地址
  發起異步請求加載該頁面地址

③把請求來的數據加載到指定的容器中

二、通過VueRouter來實現一個SPA的基本步驟

①引入對應的vue-router.js(該文件我已經上傳到我的文件中)
②指定一個盛放代碼片段的容器

<router-view></router-view>

③創建業務所需要的各個組件
④配置路由詞典
每一個路由地址的配置對象(要加載哪個頁面...)

const myRoutes = [  {path:'/myLogin',component:TestLogin},  {path:'/myRegister',component:TestRegister}  ]  const myRouter = new VueRouter({  routes:myRoutes   })  new Vue({    router:myRouter   })

⑤測試
在地址欄中 輸入對應的不同的路由地址 確認是否能夠加載對應的<!doctype html>

<html> <head> <meta charset="UTF-8"> <title></title>  <script src="js/vue.js"></script><!-- 引入文件 -->  <script src="js/vue-router.js"></script> </head> <body> <div id="container">    <p>{{msg}}</p><!--通過router-view指定盛放組件的容器 -->    <router-view></router-view>  </div>  <script>    var testLogin = Vue.component("login",{      template:`        <div>          <h1>這是我的登錄頁面</h1>        </div>      `    })    var testRegister = Vue.component("register",{      template:`        <div>          <h1>這是我的注冊頁面</h1>        </div>      `    })    //配置路由詞典    //對象數組    const  myRoutes =[    //當路由地址:地址欄中的那個路徑是myLogin訪問組件    //組件是作為標簽來用的所以不能直接在component后面使用    //要用返回值       //path:''指定地址欄為空:默認為Login頁面        {path:'',component:testLogin},      {path:'/myLogin',component:testLogin},      {path:'/myRegister',component:testRegister}    ]    const myRouter = new VueRouter({      //myRoutes可以直接用上面的數組替換      routes:myRoutes    })    new Vue({      router:myRouter,      //或者:      /*        router:new VueRouter({            routes:[              {path:'/myLogin',component:testLogin},      {path:'/myRegister',component:testRegister}            ]        })      */      el:"#container",      data:{        msg:"Hello VueJs"      }    })  </script> </body></html>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 文安县| 汨罗市| 剑川县| 泰顺县| 宁强县| 梧州市| 克山县| 汉沽区| 新民市| 嘉鱼县| 烟台市| 兴宁市| 呼图壁县| 三都| 伽师县| 城市| 镇坪县| 巴南区| 金秀| 如东县| 弥勒县| 西丰县| 陆丰市| 临夏县| 隆林| 安西县| 涟源市| 元江| 克什克腾旗| 沂南县| 三台县| 庆阳市| 红桥区| 牟定县| 博白县| 辽阳县| 长海县| 龙游县| 松江区| 德州市| 延吉市|