- Vue.js 2 Web Development Projects
- Guillaume Chau
- 262字
- 2021-07-02 22:34:32
Parent-to-child communication with Props
As we saw in the The almighty components section, our component-based app will have a tree of components, and we need them to communicate with each other. For now, we will only focus on descending, parent-to-child communication. This is accomplished with "props".
Our top-bar component needs to know who the players are, which one is currently playing, and what the current turn number is. So, we will need three props--players, currentPlayerIndex, and turn.
To add props to a component definition, use the props option. For now, we will simply list the names of our props. However, you should know that there is a more detailed notation with an object instead, which we will cover in the next chapters.
- Let's add the props to our component:
Vue.component('top-bar', {
// ...
props: ['players', 'currentPlayerIndex', 'turn'],
})
In the parent component, which is the root application, we can set the props value the exact same way we would for HTML attributes.
- Go ahead and use the v-bind shorthand to wire the props value with the app data in the main template:
<top-bar :turn="turn" :current-player-index="currentPlayerIndex"
:players="players" />
Note that since HTML is case-insensitive and by convention, it is recommended to use the kebab-case (with dashes) names of our props, and the camel-case names in the JavaScript code.
Now, we can use the props in our top-bar component just like data properties. For example, you could write something like this:
Vue.component('top-bar', {
// ...
created () {
console.log(this.players)
},
})
This would print the players array sent by the parent component (our app) in the browser console.
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- 演進式架構(原書第2版)
- 基于粒計算模型的圖像處理
- 算法零基礎一本通(Python版)
- PHP 7底層設計與源碼實現
- Python從入門到精通(精粹版)
- Java FX應用開發(fā)教程
- C語言程序設計
- Learning DHTMLX Suite UI
- 蘋果的產品設計之道:創(chuàng)建優(yōu)秀產品、服務和用戶體驗的七個原則
- Visual Basic 6.0程序設計實驗教程
- Python預測分析與機器學習
- C#程序設計基礎入門教程
- Building Clouds with Windows Azure Pack
- Android開發(fā)進階實戰(zhàn):拓展與提升