首頁  >  文章  >  web前端  >  vue中SPA單頁應用程式詳解

vue中SPA單頁應用程式詳解

php中世界最好的语言
php中世界最好的语言原創
2018-04-14 17:08:213864瀏覽

這次帶給大家vue中SPA單頁面應用程式詳解,使用vue中SPA單頁應用程式的注意事項有哪些,以下就是實戰案例,一起來看一下。

一、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 
  })
### ⑤測試###在地址列中輸入對應的不同的路由地址確認是否能夠加載對應的###

 
 
 
  

  
 
 
 

    

{{msg}}

    <router-view></router-view>   

  <script>     var testLogin = Vue.component("login",{       template:`         <p>           <h1>这是我的登录页面</h1>         </p>       `     })     var testRegister = Vue.component("register",{       template:`         <p>           <h1>这是我的注册页面</h1>         </p>       `     })     //配置路由词典     //对象数组     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>  


 
 
 SPA练习
  
  
 
 
 

    

{{msg}}

    <router-view></router-view>   

  <script>   /*     需要大家创建一个SPA,这个SPA有3个组件,分别对应的是collect/detail/order     功能需求:     在地址栏中路由地址是:     /myColllect --> 收藏页组件     /myDetail --> 详情页组件     /myOrder --> 订单页组件   */   /*     1、引入js文件     2、创建三个组件,需要返回值     3、路由词典配置(三小步)const myRoutes、const myRouter、router:myRouter,     4、指定一个盛放代码片段的容器           &lt;router-view&gt;&lt;/router-view&gt;   */     var testCollect = Vue.component("collect",{       template:`         <p>           <h1>这是收藏页</h1>         </p>       `     })     var testDetail = Vue.component("detail",{       template:`         <p>           <h1>这是详情页</h1>         </p>       `     })     var testOrder = Vue.component("order",{       template:`         <p>           <h1>这是订单页</h1>         </p>       `     })     const myRoutes = [         {path:"",component:testCollect},         {path:"/myColllect",component:testCollect},         {path:"/myDetail",component:testDetail},         {path:"/myOrder",component:testOrder},     ]     const myRouter = new VueRouter({       routes:myRoutes     })     new Vue({       router:myRouter,       el:"#container",       data:{         msg:"Hello VueJs"       }     })   </script>  
###相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章! ######推薦閱讀:#########mint-ui loadmore上拉載入與下拉刷新衝突處理方法###############微信小程式怎樣使圖片上傳至伺服器############

以上是vue中SPA單頁應用程式詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn