Communication between VueJS components





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm developing custom components on vuejs and want to reuse one component into other, how can I manage state child component from parent?



I'm trying to discover correct way of communication between components and got a situation when custom-input component doesn't change value.
For example, when I type some text into input, a text is changed in both places ({‌{testData}} and input as well), but when I click by button I expected that the value will changed in both places as well, but it doesn't work. Could somebody look the code below and explain what I do wrong?



<div id="app">
<custom v-model="testData">content</custom>
{‌{testData}}
</div>

Vue.component('customInput', {
template: '
<div>
<input type="text" :value="textValue" @input="doEvent($event)">
</div>',
props: ['value'],
data: function() {
return {
textValue: this.value
};
},
methods: {
doEvent(event) {
this.$emit("input", event.target.value);
}
}
});

Vue.component('Custom', {
template: '
<div>
<button @click="changeData()">Chnage Data</button>
<custom-input v-model="textValue" @input="doEvent()"/>
</div>',
props: ['value'],
data: function() {
return {
textValue: this.value
};
},
methods: {
doEvent() {
this.$emit("input", this.textValue);
},
changeData() {
this.textValue = 'Data has chaged';
this.doEvent();
}
}
});

new Vue({
el: '#app',
data: {
testData: "test data"
}
});


I expect that when I typed or change text by clicking the button the data will change in both places.










share|improve this question































    0















    I'm developing custom components on vuejs and want to reuse one component into other, how can I manage state child component from parent?



    I'm trying to discover correct way of communication between components and got a situation when custom-input component doesn't change value.
    For example, when I type some text into input, a text is changed in both places ({‌{testData}} and input as well), but when I click by button I expected that the value will changed in both places as well, but it doesn't work. Could somebody look the code below and explain what I do wrong?



    <div id="app">
    <custom v-model="testData">content</custom>
    {‌{testData}}
    </div>

    Vue.component('customInput', {
    template: '
    <div>
    <input type="text" :value="textValue" @input="doEvent($event)">
    </div>',
    props: ['value'],
    data: function() {
    return {
    textValue: this.value
    };
    },
    methods: {
    doEvent(event) {
    this.$emit("input", event.target.value);
    }
    }
    });

    Vue.component('Custom', {
    template: '
    <div>
    <button @click="changeData()">Chnage Data</button>
    <custom-input v-model="textValue" @input="doEvent()"/>
    </div>',
    props: ['value'],
    data: function() {
    return {
    textValue: this.value
    };
    },
    methods: {
    doEvent() {
    this.$emit("input", this.textValue);
    },
    changeData() {
    this.textValue = 'Data has chaged';
    this.doEvent();
    }
    }
    });

    new Vue({
    el: '#app',
    data: {
    testData: "test data"
    }
    });


    I expect that when I typed or change text by clicking the button the data will change in both places.










    share|improve this question



























      0












      0








      0








      I'm developing custom components on vuejs and want to reuse one component into other, how can I manage state child component from parent?



      I'm trying to discover correct way of communication between components and got a situation when custom-input component doesn't change value.
      For example, when I type some text into input, a text is changed in both places ({‌{testData}} and input as well), but when I click by button I expected that the value will changed in both places as well, but it doesn't work. Could somebody look the code below and explain what I do wrong?



      <div id="app">
      <custom v-model="testData">content</custom>
      {‌{testData}}
      </div>

      Vue.component('customInput', {
      template: '
      <div>
      <input type="text" :value="textValue" @input="doEvent($event)">
      </div>',
      props: ['value'],
      data: function() {
      return {
      textValue: this.value
      };
      },
      methods: {
      doEvent(event) {
      this.$emit("input", event.target.value);
      }
      }
      });

      Vue.component('Custom', {
      template: '
      <div>
      <button @click="changeData()">Chnage Data</button>
      <custom-input v-model="textValue" @input="doEvent()"/>
      </div>',
      props: ['value'],
      data: function() {
      return {
      textValue: this.value
      };
      },
      methods: {
      doEvent() {
      this.$emit("input", this.textValue);
      },
      changeData() {
      this.textValue = 'Data has chaged';
      this.doEvent();
      }
      }
      });

      new Vue({
      el: '#app',
      data: {
      testData: "test data"
      }
      });


      I expect that when I typed or change text by clicking the button the data will change in both places.










      share|improve this question
















      I'm developing custom components on vuejs and want to reuse one component into other, how can I manage state child component from parent?



      I'm trying to discover correct way of communication between components and got a situation when custom-input component doesn't change value.
      For example, when I type some text into input, a text is changed in both places ({‌{testData}} and input as well), but when I click by button I expected that the value will changed in both places as well, but it doesn't work. Could somebody look the code below and explain what I do wrong?



      <div id="app">
      <custom v-model="testData">content</custom>
      {‌{testData}}
      </div>

      Vue.component('customInput', {
      template: '
      <div>
      <input type="text" :value="textValue" @input="doEvent($event)">
      </div>',
      props: ['value'],
      data: function() {
      return {
      textValue: this.value
      };
      },
      methods: {
      doEvent(event) {
      this.$emit("input", event.target.value);
      }
      }
      });

      Vue.component('Custom', {
      template: '
      <div>
      <button @click="changeData()">Chnage Data</button>
      <custom-input v-model="textValue" @input="doEvent()"/>
      </div>',
      props: ['value'],
      data: function() {
      return {
      textValue: this.value
      };
      },
      methods: {
      doEvent() {
      this.$emit("input", this.textValue);
      },
      changeData() {
      this.textValue = 'Data has chaged';
      this.doEvent();
      }
      }
      });

      new Vue({
      el: '#app',
      data: {
      testData: "test data"
      }
      });


      I expect that when I typed or change text by clicking the button the data will change in both places.







      vuejs2 vue-component






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 8:51









      JeuneApprenti

      15212




      15212










      asked Jan 4 at 7:41









      P.MrP.Mr

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Your code worked perfectly! You just forgot to add backticks instead of single quotes in your template.
          Using backticks, you can insert newlines. With single or double quotes, you must write everything on oneline :)






          Vue.component('customInput', {
          template: `
          <div>
          <input type="text" :value="textValue" @input="doEvent($event)">
          </div>`,
          props: ['value'],
          data: function() {
          return {
          textValue: this.value
          };
          },
          methods: {
          doEvent(event) {
          this.$emit("input", event.target.value);
          }
          }
          });

          Vue.component('Custom', {
          template: `
          <div>
          <button @click="changeData()">Chnage Data</button>
          <custom-input v-model="textValue" @input="doEvent()"/>
          </div>`,
          props: ['value'],
          data: function() {
          return {
          textValue: this.value
          };
          },
          methods: {
          doEvent() {
          this.$emit("input", this.textValue);
          },
          changeData() {
          this.textValue = 'Data has chaged';
          this.doEvent();
          }
          }
          });

          new Vue({
          el: '#app',
          data: {
          testData: "test data"
          }
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

          <div id="app">
          <custom v-model="testData">content</custom>
          {{testData}}
          </div>








          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034854%2fcommunication-between-vuejs-components%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Your code worked perfectly! You just forgot to add backticks instead of single quotes in your template.
            Using backticks, you can insert newlines. With single or double quotes, you must write everything on oneline :)






            Vue.component('customInput', {
            template: `
            <div>
            <input type="text" :value="textValue" @input="doEvent($event)">
            </div>`,
            props: ['value'],
            data: function() {
            return {
            textValue: this.value
            };
            },
            methods: {
            doEvent(event) {
            this.$emit("input", event.target.value);
            }
            }
            });

            Vue.component('Custom', {
            template: `
            <div>
            <button @click="changeData()">Chnage Data</button>
            <custom-input v-model="textValue" @input="doEvent()"/>
            </div>`,
            props: ['value'],
            data: function() {
            return {
            textValue: this.value
            };
            },
            methods: {
            doEvent() {
            this.$emit("input", this.textValue);
            },
            changeData() {
            this.textValue = 'Data has chaged';
            this.doEvent();
            }
            }
            });

            new Vue({
            el: '#app',
            data: {
            testData: "test data"
            }
            });

            <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

            <div id="app">
            <custom v-model="testData">content</custom>
            {{testData}}
            </div>








            share|improve this answer




























              0














              Your code worked perfectly! You just forgot to add backticks instead of single quotes in your template.
              Using backticks, you can insert newlines. With single or double quotes, you must write everything on oneline :)






              Vue.component('customInput', {
              template: `
              <div>
              <input type="text" :value="textValue" @input="doEvent($event)">
              </div>`,
              props: ['value'],
              data: function() {
              return {
              textValue: this.value
              };
              },
              methods: {
              doEvent(event) {
              this.$emit("input", event.target.value);
              }
              }
              });

              Vue.component('Custom', {
              template: `
              <div>
              <button @click="changeData()">Chnage Data</button>
              <custom-input v-model="textValue" @input="doEvent()"/>
              </div>`,
              props: ['value'],
              data: function() {
              return {
              textValue: this.value
              };
              },
              methods: {
              doEvent() {
              this.$emit("input", this.textValue);
              },
              changeData() {
              this.textValue = 'Data has chaged';
              this.doEvent();
              }
              }
              });

              new Vue({
              el: '#app',
              data: {
              testData: "test data"
              }
              });

              <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

              <div id="app">
              <custom v-model="testData">content</custom>
              {{testData}}
              </div>








              share|improve this answer


























                0












                0








                0







                Your code worked perfectly! You just forgot to add backticks instead of single quotes in your template.
                Using backticks, you can insert newlines. With single or double quotes, you must write everything on oneline :)






                Vue.component('customInput', {
                template: `
                <div>
                <input type="text" :value="textValue" @input="doEvent($event)">
                </div>`,
                props: ['value'],
                data: function() {
                return {
                textValue: this.value
                };
                },
                methods: {
                doEvent(event) {
                this.$emit("input", event.target.value);
                }
                }
                });

                Vue.component('Custom', {
                template: `
                <div>
                <button @click="changeData()">Chnage Data</button>
                <custom-input v-model="textValue" @input="doEvent()"/>
                </div>`,
                props: ['value'],
                data: function() {
                return {
                textValue: this.value
                };
                },
                methods: {
                doEvent() {
                this.$emit("input", this.textValue);
                },
                changeData() {
                this.textValue = 'Data has chaged';
                this.doEvent();
                }
                }
                });

                new Vue({
                el: '#app',
                data: {
                testData: "test data"
                }
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

                <div id="app">
                <custom v-model="testData">content</custom>
                {{testData}}
                </div>








                share|improve this answer













                Your code worked perfectly! You just forgot to add backticks instead of single quotes in your template.
                Using backticks, you can insert newlines. With single or double quotes, you must write everything on oneline :)






                Vue.component('customInput', {
                template: `
                <div>
                <input type="text" :value="textValue" @input="doEvent($event)">
                </div>`,
                props: ['value'],
                data: function() {
                return {
                textValue: this.value
                };
                },
                methods: {
                doEvent(event) {
                this.$emit("input", event.target.value);
                }
                }
                });

                Vue.component('Custom', {
                template: `
                <div>
                <button @click="changeData()">Chnage Data</button>
                <custom-input v-model="textValue" @input="doEvent()"/>
                </div>`,
                props: ['value'],
                data: function() {
                return {
                textValue: this.value
                };
                },
                methods: {
                doEvent() {
                this.$emit("input", this.textValue);
                },
                changeData() {
                this.textValue = 'Data has chaged';
                this.doEvent();
                }
                }
                });

                new Vue({
                el: '#app',
                data: {
                testData: "test data"
                }
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

                <div id="app">
                <custom v-model="testData">content</custom>
                {{testData}}
                </div>








                Vue.component('customInput', {
                template: `
                <div>
                <input type="text" :value="textValue" @input="doEvent($event)">
                </div>`,
                props: ['value'],
                data: function() {
                return {
                textValue: this.value
                };
                },
                methods: {
                doEvent(event) {
                this.$emit("input", event.target.value);
                }
                }
                });

                Vue.component('Custom', {
                template: `
                <div>
                <button @click="changeData()">Chnage Data</button>
                <custom-input v-model="textValue" @input="doEvent()"/>
                </div>`,
                props: ['value'],
                data: function() {
                return {
                textValue: this.value
                };
                },
                methods: {
                doEvent() {
                this.$emit("input", this.textValue);
                },
                changeData() {
                this.textValue = 'Data has chaged';
                this.doEvent();
                }
                }
                });

                new Vue({
                el: '#app',
                data: {
                testData: "test data"
                }
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

                <div id="app">
                <custom v-model="testData">content</custom>
                {{testData}}
                </div>





                Vue.component('customInput', {
                template: `
                <div>
                <input type="text" :value="textValue" @input="doEvent($event)">
                </div>`,
                props: ['value'],
                data: function() {
                return {
                textValue: this.value
                };
                },
                methods: {
                doEvent(event) {
                this.$emit("input", event.target.value);
                }
                }
                });

                Vue.component('Custom', {
                template: `
                <div>
                <button @click="changeData()">Chnage Data</button>
                <custom-input v-model="textValue" @input="doEvent()"/>
                </div>`,
                props: ['value'],
                data: function() {
                return {
                textValue: this.value
                };
                },
                methods: {
                doEvent() {
                this.$emit("input", this.textValue);
                },
                changeData() {
                this.textValue = 'Data has chaged';
                this.doEvent();
                }
                }
                });

                new Vue({
                el: '#app',
                data: {
                testData: "test data"
                }
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

                <div id="app">
                <custom v-model="testData">content</custom>
                {{testData}}
                </div>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 4 at 9:58









                Michele RivaMichele Riva

                368314




                368314
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034854%2fcommunication-between-vuejs-components%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas