2017-08-03 54 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("/") 
      } 
     })