vuejs2 - Why v-bind is one-way data binding while v-for can update data from children component in vue.js? -


guys. reading book of majesty of vue.js 2. confused 1 example in book.

my question - why upvote button can modify data of vue instance displayed in pre tag while favorite button can not?

it said favorite bound via v-bind directive, 1 way data binding means children not able sync data parent. how did story updated? 2 way data binding v-model?

here code example:

<!doctype html> <html lang="en"> <head>     <meta charset="utf-8">     <title>hello vue</title> </head> <body> <div v-cloak id="app">     <div class="container">         <h1>let's hear stories!</h1>         <ul class="list-group">             <story v-for="story in stories" :story="story" :favorite="favorite"></story>         </ul>         <pre>{{ $data }}</pre>     </div> </div> </body> <template id="story-template">     <li class="list-group-item">         {{ story.writer }} said "{{ story.plot }}"         story upvotes {{ story.upvotes }}.         <button v-show="!story.voted" @click="upvote" class="btn btn-default">upvote</button>         <button v-show="!isfavorite" @click="setfavorite" class="btn btn-primary">favorite</button>      </li> </template> <script src="../../vue.js"></script> <script type="text/javascript">     vue.component('story', {         template: "#story-template",          props: ['story', 'favorite'],         methods: {             upvote: function () {                 this.story.upvotes += 1;                 this.story.voted = true;             },             setfavorite: function () {                  this.favorite = this.story;             }         },         computed: {             isfavorite: function () {                 return this.story === this.favorite             }         }     });     window.app = new vue({         el: '#app',         data: {             stories: [                 {                     plot: 'my horse amazing.',                     writer: 'mr. weebl',                     upvotes: 28,                     voted: false                 },                 {                     plot: 'narwhals invented shish kebab.',                     writer: 'mr. weebl',                     upvotes: 8,                     voted: false                 },                 {                     plot: 'the dark side of force stronger.',                     writer: 'darth vader',                     upvotes: 49,                     voted: false                 },                 {                     plot: 'one not walk mordor',                     writer: 'boromir',                     upvotes: 74,                     voted: false                 }             ],             favorite: {}         }     }) </script> </html> 

this has how objects work in javascript. when stored in variable sir reference object. when pass around, you're passing reference. meaning altering object (not overwriting!), alters on places.

what happens in example modify story object. alter keys not overwrite object itself. seeing app has same reference story object. changes shown.

in case of favorite however. passed reference of favorite object. click favorite button. swaps variable reference story, locally. app still holds old reference. because pass reference , not parent object.

this state managers vuex come in place.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -