2017-08-03 57 views
1

之前,我有這樣的問題如何從服務器獲取數據的稱號
如何阻止安裝該組件在<router-view>直到服務器或如何獲得之前的數據接收數據組件被安裝到<router-view>組件安裝vue2.0

我的文件:

1 main.js

new Vue({ 
    el: '#app', 
    router, 
    components: { Login, Start }, 
    data: function(){ 
    return{ 
     info: null, 
    } 
    }, 
    methods:{ 
    bef: function(){ 
     this.$http.get('xxx').then(function(response){ 
     return response.body 
     }); 
    } 
    }, 
    beforeMount(){ 
    this.info= this.bef() 
    } 
}) 

第二組件文件Comp.vue

export default{ 
    name: 'start', 
    data(){ 
     return{ 

     } 
    }, 
    beforeMount(){ 
     console.log(this.$parent.info) 
    } 
} 

如何正確地獲取非空值,但服務器響應?
預先感謝您

+0

與解決: 返回新的承諾((解析,拒絕)=> { }) ; – Proxr

回答

0

解決了與:

checkLogged: function() { 
     return new Promise((resolve,reject) => { 
      this.$http.get('xxx').then(response => { 
      if (response.body == 1) { 
       resolve(true); 
      } else { 
       resolve(false); 
      } 
      }, response => { 
      reject('connection failure'); 
      }); 
     }); 
    }, 

,並在第二個選項:

this.$root.checkLogged().then((response) => { 
      if(response){ 
       this.logged = true 
      }else{ 
       router.push("/") 
      } 
     })