Home >Web Front-end >JS Tutorial >Analysis on the strength of value transfer between Vue components
When we get started with Vue, we have to mention components. In some cases, components need to pass values to each other. For example, a parent component needs to pass a value to a child component, and a child component needs to pass a value to the parent component. Then the following Use a strength to elaborate.
Effect:
We want to achieve such an effect. After entering text in the input box and clicking the submit button, the corresponding input content will appear below. If you click on something, the content disappears.
As shown below, for example, when we click 2, 2 will disappear
##Analysis:
1. First, we will get the input content, collect all the input content into an array, and loop the data in<div id="root"> <input v-model="inputValue" /> <button @click="handleSubmit">提交</button> <ul> <todo-item v-bind:content="item" v-bind:index="index" v-for="(item,index) in list" @delete="handleItemDelete" ></todo-item> </ul> </div> <script> var TodoItem={ props:['content','index'], template:"<li @click='handleItemClick'>{{content}}</li>", methods:{ handleItemClick:function(){ this.$emit('delete',this.index); } } } new Vue({ el:"#root", data:{ inputValue:'', list:[] }, components:{ 'TodoItem':TodoItem }, methods:{ handleSubmit:function(){ this.list.push(this.inputValue) this.inputValue = '' //每次提交后清空 }, handleItemDelete:function(index){ this.list.splice(index,1); } } }) </script>Through the code, it is not difficult to find that the main value passed from the subcomponent to the parent component is here:
this.$emit('delete',this.index);In addition, it should be noted that in Vue, everything starting with $ is called vue Instance attributes or methods, in addition to this, you also need to pay attention to some abbreviations, such as and
The above is the detailed content of Analysis on the strength of value transfer between Vue components. For more information, please follow other related articles on the PHP Chinese website!