vue-router?導(dǎo)航完成后獲取數(shù)據(jù)的實(shí)現(xiàn)方法
created:實(shí)例已經(jīng)創(chuàng)建完成之后被調(diào)用。在這一步,實(shí)例已完成以下的配置:數(shù)據(jù)觀測(cè)(data observer),屬性和方法的運(yùn)算, watch/event 事件回調(diào)。然而,掛載階段還沒(méi)開(kāi)始,$el 屬性目前不可見(jiàn)。
使用生命周期的 created() 函數(shù),在組件創(chuàng)建完成后調(diào)用該方法。
created(){
// 組件創(chuàng)建完成后獲取數(shù)據(jù)
// 此時(shí)data已經(jīng)被監(jiān)聽(tīng)了
this.fetchData();
},
watch:{
'$route':'fetchData'
},
methods:{
fetchData(){
this.error = null;
this.post = null;
this.loading = true;
this.$axios.get('http://127.0.0.1:8888/post')
.then(res=>{
this.loading = false;
console.log(res.data);
this.post = res.data;
})
.catch(err=>{
this.error = err.toString();
})
}
}完整代碼如下:
<div id="app"><div>
<!-- 導(dǎo)航完成后獲取數(shù)據(jù),讓我們?cè)跀?shù)據(jù)獲取期間可以展示loading....狀態(tài) -->
<script type="text/javascript" src="./vue.js"></script>
<script type="text/javascript" src="./axios.js"></script>
<script type="text/javascript" src="./vue-router.js"></script>
<script type="text/javascript">
var Index = {
template:`
<div>我是首頁(yè)</div>
`
};
var Post = {
data(){
return{
loading:false,
error:null,
post:null
}
},
template:`
<div>
<div class='loading' v-if='loading'>
loading...
</div>
<div class='error' v-if='error'>
{{error}}
</div>
<div class='content' v-if='post'>
<h2>{{post.title}}</h2>
<p>{{post.body}}</p>
</div>
</div>
`,
created(){
// 組件創(chuàng)建完成后獲取數(shù)據(jù)
// 此時(shí)data已經(jīng)被監(jiān)聽(tīng)了
this.fetchData();
},
watch:{
'$route':'fetchData'
},
methods:{
fetchData(){
this.error = null;
this.post = null;
this.loading = true;
this.$axios.get('http://127.0.0.1:8888/post')
.then(res=>{
this.loading = false;
console.log(res.data);
this.post = res.data;
})
.catch(err=>{
this.error = err.toString();
})
}
}
}
var router = new VueRouter({
routes:[
{
path:'/index',
name:'index',
component:Index
},
{
path:'/post',
name:'post',
component:Post
}
]
});
var App = {
template:`
<div>
<router-link :to = "{name:'index'}">首頁(yè)</router-link>
<router-link :to = "{name:'post'}">我的博客</router-link>
<router-view></router-view>
</div>
`
};
Vue.prototype.$axios = axios;
new Vue({
el:'#app',
template:`<App/>`,
components:{
App
},
router
});
</script>到此這篇關(guān)于vue-router 導(dǎo)航完成后獲取數(shù)據(jù)的文章就介紹到這了,更多相關(guān)vue-router 獲取數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用vue-router在Vue頁(yè)面之間傳遞數(shù)據(jù)的方法
- 詳解vue-router數(shù)據(jù)加載與緩存使用總結(jié)
- Vue + Vue-router 同名路由切換數(shù)據(jù)不更新的方法
- vue-router之實(shí)現(xiàn)導(dǎo)航切換過(guò)渡動(dòng)畫(huà)效果
- vue-router二級(jí)導(dǎo)航切換路由及高亮顯示的實(shí)現(xiàn)方法
- 詳解vue-router導(dǎo)航守衛(wèi)
- vue-router路由與頁(yè)面間導(dǎo)航實(shí)例解析
- vue-router 導(dǎo)航鉤子的具體使用方法
相關(guān)文章
vue項(xiàng)目純前端實(shí)現(xiàn)的模板打印功能示例代碼
在Vue項(xiàng)目中,通過(guò)使用vue-print-nb插件,可以實(shí)現(xiàn)頁(yè)面的打印功能,這篇文章主要介紹了vue項(xiàng)目純前端實(shí)現(xiàn)的模板打印功能的相關(guān)資料,需要的朋友可以參考下2024-10-10
vue如何給element-ui中的el-tabs動(dòng)態(tài)設(shè)置label屬性
這篇文章主要介紹了vue如何給element-ui中的el-tabs動(dòng)態(tài)設(shè)置label屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue3進(jìn)階主題Composition API使用詳解
這篇文章主要為大家介紹了Vue3進(jìn)階主題Composition API使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
vue實(shí)現(xiàn)button按鈕的重復(fù)點(diǎn)擊指令方式
這篇文章主要介紹了vue實(shí)現(xiàn)button按鈕的重復(fù)點(diǎn)擊指令方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue清除定時(shí)器setInterval優(yōu)化方案分享
這篇文章主要介紹了Vue清除定時(shí)器setInterval優(yōu)化方案分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue中.vue文件比main.js先執(zhí)行的問(wèn)題及解決
這篇文章主要介紹了Vue中.vue文件比main.js先執(zhí)行的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12

