vue中SPA單頁面應(yīng)用程序詳解
一、SPA的概述
SPA(single page application)單頁面應(yīng)用程序,在一個(gè)完成的應(yīng)用或者站點(diǎn)中,只有一個(gè)完整的html頁面,這個(gè)頁面有一個(gè)容器,可以把需要加載的代碼片段插入到該容器中。
SPA的工作原理:
eg: http://127.0.0.1/index.html#/start
①根據(jù)地址欄中url解析完整的頁面:index.html
加載index.html
②根據(jù)地址欄中url解析#后的路由地址: start
根據(jù)路由地址,去在當(dāng)前應(yīng)用的配置中 找該路由地址的配置對(duì)象去查找該路由地址 所對(duì)應(yīng)的模板的頁面地址
發(fā)起異步請(qǐng)求加載該頁面地址
③把請(qǐng)求來的數(shù)據(jù)加載到指定的容器中
二、通過VueRouter來實(shí)現(xiàn)一個(gè)SPA的基本步驟
①引入對(duì)應(yīng)的vue-router.js(該文件我已經(jīng)上傳到我的文件中)
②指定一個(gè)盛放代碼片段的容器
<router-view></router-view>
③創(chuàng)建業(yè)務(wù)所需要的各個(gè)組件
④配置路由詞典
每一個(gè)路由地址的配置對(duì)象(要加載哪個(gè)頁面...)
const myRoutes = [
{path:'/myLogin',component:TestLogin},
{path:'/myRegister',component:TestRegister}
]
const myRouter = new VueRouter({
routes:myRoutes
})
new Vue({
router:myRouter
})
⑤測試
在地址欄中 輸入對(duì)應(yīng)的不同的路由地址 確認(rèn)是否能夠加載對(duì)應(yīng)的<!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>這是我的注冊(cè)頁面</h1>
</div>
`
})
//配置路由詞典
//對(duì)象數(shù)組
const myRoutes =[
//當(dāng)路由地址:地址欄中的那個(gè)路徑是myLogin訪問組件
//組件是作為標(biāo)簽來用的所以不能直接在component后面使用
//要用返回值
//path:''指定地址欄為空:默認(rèn)為Login頁面
{path:'',component:testLogin},
{path:'/myLogin',component:testLogin},
{path:'/myRegister',component:testRegister}
]
const myRouter = new VueRouter({
//myRoutes可以直接用上面的數(shù)組替換
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>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>SPA練習(xí)</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>
</div>
<script>
/*
需要大家創(chuàng)建一個(gè)SPA,這個(gè)SPA有3個(gè)組件,分別對(duì)應(yīng)的是collect/detail/order
功能需求:
在地址欄中路由地址是:
/myColllect --> 收藏頁組件
/myDetail --> 詳情頁組件
/myOrder --> 訂單頁組件
*/
/*
1、引入js文件
2、創(chuàng)建三個(gè)組件,需要返回值
3、路由詞典配置(三小步)const myRoutes、const myRouter、router:myRouter,
4、指定一個(gè)盛放代碼片段的容器
<router-view></router-view>
*/
var testCollect = Vue.component("collect",{
template:`
<div>
<h1>這是收藏頁</h1>
</div>
`
})
var testDetail = Vue.component("detail",{
template:`
<div>
<h1>這是詳情頁</h1>
</div>
`
})
var testOrder = Vue.component("order",{
template:`
<div>
<h1>這是訂單頁</h1>
</div>
`
})
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>
</body>
</html>以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解vuex中的this.$store.dispatch方法
- Vue實(shí)現(xiàn)數(shù)據(jù)表格合并列rowspan效果
- 在Vuex使用dispatch和commit來調(diào)用mutations的區(qū)別詳解
- Vue.js實(shí)現(xiàn)一個(gè)SPA登錄頁面的過程【推薦】
- 基于Vue的SPA動(dòng)態(tài)修改頁面title的方法(推薦)
- 詳解vue 單頁應(yīng)用(spa)前端路由實(shí)現(xiàn)原理
- 淺談Vue SPA 首屏加載優(yōu)化實(shí)踐
- VUE解決微信簽名及SPA微信invalid signature問題(完美處理)
- Vue SPA 路由跳轉(zhuǎn)無法回到頂部問題排查與解決
相關(guān)文章
vue實(shí)現(xiàn)登錄時(shí)滑塊驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)登錄時(shí)滑塊驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Vue2項(xiàng)目中Mock.js的完整集成與使用教程
Mock.js 是一個(gè)可以在開發(fā)階段模擬后端數(shù)據(jù)接口的 JavaScript 庫,它能夠生成大量不同類型的隨機(jī)數(shù)據(jù),并且模擬真實(shí)的接口返回,允許前端開發(fā)在沒有真實(shí)后端接口的情況下進(jìn)行開發(fā),本文給大家介紹了Vue2項(xiàng)目中Mock.js的完整集成與使用教程,需要的朋友可以參考下2025-02-02
詳解unplugin?vue?components不能識(shí)別組件自動(dòng)導(dǎo)入類型pnpm
這篇文章主要為大家介紹了unplugin?vue?components不能識(shí)別組件自動(dòng)導(dǎo)入類型pnpm詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Delete `,` 如何解決(vue eslint與prettier沖突)
這篇文章主要介紹了Delete `,` 如何解決(vue eslint與prettier沖突)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

