2017-12-18 184 views
4

我想將道具從父組件傳遞給子組件。我的道具是tid將axios中的道具傳遞給Vue.js?

這是父組件:

<div id="tracksec" class="panel-collapse collapse"> 
    <library :tid="track.category_id"></library> 
</div> 

這是子組件:

<script> 
import Chapter from "./chapter"; 
import Http from "../../services/http/httpService"; 
export default { 
    components: {Chapter}, 
    props:['tid'], 
    name: "library", 
    data(){ 
     return{ 
      library:{}, 
     } 
    }, 
    computed:{ 
     getPr(){ 
      return this.$props; 
     } 
    }, 
     mounted(){ 
     console.log(this.$props); 
     Http.get('/api/interactive/lib/' + this.tid) 
      .then(response => this.library = response.data) 
      .catch(error => console.log(error.response.data)) 
    } 
} 

這是HTTP來源:

import axios from 'axios'; 


class httpService { 

static get(url, params) { 
    if (!params) { 
     return axios.get(url); 
    } 
    return axios.get(url, { 
     params: params 
    }); 
} 

static post(url, params) { 
    return axios.post(url, params); 
} 
} 

export default httpService; 

我想通過tid值爲http.get函數。例如:

Http.get('/api/interactive/lib/' + this.tid) 

然而tid值爲undefined。 如何在掛載或創建的掛鉤中獲得tid

+0

一切看起來我的權利。您的父級中定義了「track.category_id」嗎? –

回答

1

我是Vue的新手,但我想你可能想添加一個觸發變化的「觀察者」。你的「軌道對象」在創建時是空的,這個領先的track.category_id是未定義的。然後,當您從HTTP獲取答案時,您的值已設置,但未在庫組件中更新。

事情是這樣的:

<script> 
import Chapter from "./chapter"; 
import Http from "../../services/http/httpService"; 
export default { 
    components: {Chapter}, 
    props:['tid'], 
    name: "library", 
    data(){ 
     return{ 
      library:{}, 
     } 
    }, 
    watch: { 
     // if tid is updated, this will trigger... I Think :-D 
     tid: function (value) { 
      console.log(value); 
      Http.get('/api/interactive/lib/' + value) 
      .then(response => this.library = response.data) 
      .catch(error => console.log(error.response.data)) 
     } 
     }, 
    computed:{ 
     getPr(){ 
      return this.$props; 
     } 
    }, 
     mounted(){ 
     console.log(this.$props); 

     // Will be undefined 
     /* 
     Http.get('/api/interactive/lib/' + this.tid) 
      .then(response => this.library = response.data) 
      .catch(error => console.log(error.response.data))*/ 
    } 
} 
</script> 

Documentation about watchers

(廣東話測試代碼,但你可能會得到的想法)

+1

坦克兄弟,解決了 –

1

在父組件嘗試將此代碼片段

<div id="tracksec" class="panel-collapse collapse"> 
    <library tid="track.category_id"></library> 
</div> 

,然後在子組件代碼將是這樣的:

<script> 
    import Chapter from "./chapter"; 
    import Http from "../../services/http/httpService"; 
    export default { 
    components: { 
     Chapter 
    }, 
    props: [ 'tid' ], 
    name: 'library', 
    data() { 
     return { 
     library: {}, 
     } 
    }, 
    computed: { 
     getPr() { 
      return this.tid; 
     } 
    }, 
    mounted() { 
     const tidValue = this.tid // get the value of props you've passed 
     console.log(tidValue) // check if it is getting the right value. 
     // code for http request. 
    } 
} 
</script> 
+0

tid是動態不靜態的,是我的項目。 –

+0

@Mohammadnajjary是動態的,只有道具的名字是靜態的嗎?,'tid'的值是動態的 –

+0

你的回答是undefined –

0

這是父腳本部分:

import Library from "./library"; 
import Http from '../../services/http/httpService'; 

export default { 
    components: {Library}, 
    name: "interactive", 
    data() { 
     return { 
      track: {}, 
     } 
    }, 

    mounted() { 
     Http.get('/api/interactive/track/' + this.$route.params.id) 
      .then(response => this.track = response.data) 
      .catch(error => console.log(error.response.data)) 
    } 
} 
+0

你用過我提供的腳本嗎?你可以使用'this.youPropsName'來檢查你通過的道具,你說過「我想把tid值傳遞給http.get函數」。你可以通過使用'this.tid'來檢查'tid'的值,它放在Vue –

+0

的'mounted()'或'created()'生命週期鉤子的子組件中,我確實是這樣做的,但是this.tid是未定義的。 –

+0

您是否嘗試過檢查「track.category_id」的值是否未定義? –

相關問題