Question 11
Consider the below application with markup “index.html” and javascript file “app.js”.
index.html:
<body> <div id="app"></div> <scriptsrc="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> <script src="app.js" type="module"></script></body>app.js:
const Player = { template: `<div><div>{{striker.name}}*</div> <div>{{nonStriker.name}}</div> </div>`, data() { return { player1: {name: 'Rohit'}, player2: {name: 'Virat'}, } }, props: { run: { type: Number, default: 0 }, }, computed: { striker() { return this.run % 2 == 0 ? this.player1 : this.player2 }, nonStriker() { return this.run % 2 == 1 ? this.player1 : this.player2 }, },}
const Run = { template: `<button @click="$emit('run-changed',run)">{{run}}</button>`, props: { run: Number },}
new Vue({ el: '#app', template: `<div> <Player :run='score'/> <div style='display: flex'> <Run :run="3" @run-changed = 'runChanged'/> <Run :run="4" @run-changed = 'runChanged' /> </div> </div>`, components: { Run, Player }, data: { score: 0, }, methods: { runChanged(run) { this.score = run }, },})Suppose the user clicks on buttons with text “3” and then “4”, what will be rendered inside the “Player” component?