2017-09-23 6 views
0

データにprops値を割り当て、このデータ値をマウント内に使用したい。今、私のアプローチが動作していないされている:マウントされたvue js内のデータ値の使用方法

dashboard.vue

<template> 
    <div> 
     <navigation-section :userid='userID' 
          :username='username' 
          :profilepic='profile_pic' 
          :unreads='unreads'> 
     </navigation-section> 
    </div> 
</template> 

<script> 
    import NavigationSection from '../components/NavigationSection'; 

    export default { 
     components: { 
      NavigationSection, 
     }, 

     data() { 
      return { 
       posts: [], 
       userID: '', 
       username: '', 
       unreads: [], 
       profile_pic: '', 
      } 
     }, 

     created() { 
      this.getNavInfo(); 
     }, 

     methods: { 
      getNavInfo() { 
       axios.get('get_nav_info').then(response => { 
        this.userID = response.data.userID; 
        this.username = response.data.username; 
        this.profile_pic = response.data.profile_pic; 
        this.unreads = response.data.unreads; 
       }) 
      } 
     } 
    } 
</script> 

NotificationSection(dashboard.vueの子)

<template> 
    <div> 
     <notification :userid='userid' :unreads='unreads'></notification> 
     <h1>{{ username }}</h1>  
     <img :src='profilepic' :alt='profilepic'> 


    </div> 
</template> 

<script> 
    import Notification from '../components/Notification'; 

    export default { 
     props:['userid', 'username', 'unreads', 'profilepic'], 
     components: { 
      Notification 
     } 
    } 
</script> 

通知(NotificationSectionの子を.vue)

<template> 
    <div> 
     // values shows in template 
     unreads array length: <div class="count" v-if='unreads.length > 0'>{{ unreads.length }}</div> 
     userid : <p>{{ userid }}</p> 
    </div> 
</template> 

<script> 
    export default { 
     props:['unreads', 'userid'], 

     computed: { 
      userID() { 
       return this.userid 
      } 
     }, 

     mounted() { 
      console.log(this.userID); // ** problem : in console.log shows a blank instead of id which is working on template. 
     } 
    } 

</script> 

enter image description here

私は質問を編集しました。値はテンプレートに表示されていますが、マウントされた内部またはイベント内の小道具の値を使用したい場合は、スクリプト内のデータ値にアクセスしようとすると動作しません。助けてくれてありがとう。

+0

問題が掲載コードにあるように表示されません。 – Bert

+0

あなたのコードは大丈夫ですか? @Bert – WahidSherief

+0

はい。ここに例があります。追加されるのはテンプレートだけです。できます。 https://codepen.io/Kradek/pen/jGVPvK?editors=1010 – Bert

答えて

0

あなたは計算されたプロパティを使用することができます。

export default { 
    props: ['userid'], 

    computed: { 
     userID() { 
      return this.userid 
     } 
    }, 

    mounted() { 
     console.log(this.userID); 
    } 
} 

それとも単にを:

export default { 
    props: ['userid'], 

    mounted() { 
     console.log(this.userid); 
    } 
} 
+0

私はマウントされているか、またはIDを作成しようとしています。これは本当に奇妙です。テンプレート内に表示されているので、( – WahidSherief

+0

親コンポーネントテンプレートで質問を更新できますか? – Ikbel

+0

親コードで質問を編集しました。親切に見てください@Ikbel – WahidSherief

関連する問題