2017-08-03 9 views
1

タイトルのような質問があります
<router-view>にコンポーネントのマウントを停止する方法は、サーバーからデータを受信するまで、またはコンポーネントがマウントされる前にサーバーからデータを取得する方法

第一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

:コンポーネントが <router-view>

マイファイルに実装されています

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

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

null値ではなくサーバーからの応答を取得する方法は適切ですか?

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'); 
      }); 
     }); 
    }, 

と第二のファイルに:
は事前にありがとう

+0

で解決:新しい約束を返す ((解決、拒否)=> { }) ; – Proxr

答えて

0

で解決

this.$root.checkLogged().then((response) => { 
      if(response){ 
       this.logged = true 
      }else{ 
       router.push("/") 
      } 
     }) 
関連する問題