- Vue.js 2 Web Development Projects
- Guillaume Chau
- 217字
- 2021-07-02 22:34:32
Props in our template
We will now use the props we created in the template of the top-bar component.
- Change the top-bar template to display the player's name with the players prop:
template: `<p class="top-bar">
<p class="player p0">{{ players[0].name }}</p>
<p class="player p1">{{ players[1].name }}</p>
</p>`,
As you can see in the preceding code, we are also using the props like we did with properties in templates. You should see the player names displayed in the app.
- Continue with the turn counter between players using the turn prop:
template: `<p class="top-bar">
<p class="player p0">{{ players[0].name }}</p>
<p class="turn-counter">
<p class="turn">Turn {{ turn }}</p>
</p>
<p class="player p1">{{ players[1].name }}</p>
</p>`,
In addition to the label, we also want to display a big arrow facing the current player to make it more obvious.
- Add the arrow image inside the .turn-counter element, and add a dynamic class using the currentPlayerIndex prop with the v-bind shorthand we used in Chapter 2, Markdown Notebook:
template: `<p class="top-bar" :class="'player-' +
currentPlayerIndex">
<p class="player p0">{{ players[0].name }}</p>
<p class="turn-counter">
<img class="arrow" src="svg/turn.svg" />
<p class="turn">Turn {{ turn }}</p>
</p>
<p class="player p1">{{ players[1].name }}</p>
</p>`,
Now, the app should display the fully featured top bar, with the two players, names and the turn counter between them. You can test the Vue-automated reactivity by typing these commands into the browser console:
state.currentPlayerIndex = 1
state.currentPlayerIndex = 0
You should see the arrow turning around to face the correct player name, which gets emphasized:

推薦閱讀
- The Android Game Developer's Handbook
- Unity Virtual Reality Projects
- Learning Laravel's Eloquent
- HTML 5與CSS 3權(quán)威指南(第3版·上冊)
- Web前端應(yīng)用開發(fā)技術(shù)
- Essential C++(中文版)
- Python物理建模初學(xué)者指南(第2版)
- HTML5與CSS3權(quán)威指南
- 3D Printing Designs:Octopus Pencil Holder
- 從零開始構(gòu)建深度前饋神經(jīng)網(wǎng)絡(luò):Python+TensorFlow 2.x
- React.js實戰(zhàn)
- Visual C++程序開發(fā)范例寶典
- 熱處理常見缺陷分析與解決方案
- 面向?qū)ο蟪绦蛟O(shè)計及C++實驗指導(dǎo)(第3版)
- Learning Network Programming with Java