2016-12-06 95 views
0

我有一個VueComponent,我需要傳遞一個值「A」。問題是值A可以通過VueRouter參數或通過模板插值。通過這兩個選項設置房產數據的最佳方式是什麼?設置屬性/數據在VueJs組件

例如

<component :property="value"></component> 

如果我瀏覽到

/組件/ 8 /編輯

mounted() { 
    this.property = this._route.params.id 
} 

回答

1

你必須搞清楚你想給優先,如果你想哪個參數優先route param,你可以做以下:

mounted() { 
    this.property = this.$route.params.id ? this.$route.params.id : this.property 
} 

如果你想傳遞給道具爲主,你可以這樣寫:

mounted() { 
    this.property = this.property ? this.property : this.$route.params.id 
} 
+0

輕微的題外話,但相關的可以在「數據」屬性是一種功能,當在模板語法參考?即'data {return {property(){return this.otherProperty}}}'或者它應該被計算出來,因爲我想要做你所說的並且能夠根據需要重寫 – Kendall

+0

在聊天中爲你留言 – Kendall