2017-02-03 9 views
1

私は私のメインコンポーネントでこれを使用しました:Vue.jsプロパティは未定義ですか?

<contacts :corporation="corporation"></contacts> 

連絡コンポーネント:

export default { 

    props: { 
     corporation: { 
      default:() => [] 
     } 
    }, 

    data() { 
     return { 
      contacts: [] 
     } 
    }, 

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

    methods: { 
     fetchContacts() { 
      console.log(this.corporation.slug); // undefined! 
      CorporationService.users(this.corporation.slug) 
       .then(({data}) => { 
        this.contacts = data.contacts; 
       }); 
     } 
    } 
} 

私は連絡コンポーネント内の連絡先を取得しようとしています。問題は、方法fetchContacts();console.log(this.corporation.slug);の場合、corporation.slugは未定義です!

私はvue devtoolsを調べると、法人小道具が適切に設定されています!

何が起こっている可能性がありますか?すでに変更しようとしました:

created() { 
    this.fetchContacts(); 
} 

mounted() { 
     this.fetchContacts(); 
    } 

にしかし、それは働いていません。

+0

親には 'corporation'変数はどのように設定されていますか? – Saurabh

+0

法人:{ default :()=> []なぜあなたはdefaultを関数として定義していますか? –

答えて

1

あなたはwatchを使用することはできますか?

このようなものです。

watch: { 
    'corporation.slug': function(slug) { 
     if(slug){ 
      this.fetchContacts(); 
     } 
    } 
} 

親コンポーネントが変更された場合は、corporation.slug子コンポーネントが自動的に連絡先を取得します。

-2

あなたの小道具のデフォルト値は、値か関数かどうかです。それが関数の場合しかし、それは何かを返すようにしています

props: { 
    corporation: { 
     default() { 
      return [] 
     } 
    } 
}, 
+0

あなたは間違いなくES6矢印関数を理解していません:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions –

+0

Wooops!それを逃した!..別のコーヒーをつかまえなければならない:)あなたは完全に正しいです、ありがとう@MU – Clorichel

+0

あなたは大歓迎です。 Upvoteようこそ;) –

関連する問題