How to keep data in component when using v-with in Vue js












1















So here is my problem:



I want to make a component that takes it's values from v-with="values" and add them to my component model after some modification, then display those modified properties.



But from what I understand, when I set values with "v-with", component data are erased so the binding between my component data (not v-with one) and my directives are lost.



I'm really new to this framework, I don't see any solution, so I guess it was time to ask my first question here !



Here is the HTML:



<script type="text/x-template" id="my-template">
<p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
<p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
</script>

<div id="myVue">
<my-component v-with="ParentData" ></my-component>
</div>


And here is the Javascript:



Vue.component('my-component', {
template: '#my-template',
data: function () {
return {
ChildMsg: "wololo"
}
},
methods:{
reloadParentMsg : function(){
this.ParentMsg="Parent";
console.log(this.ParentMsg);
},
reloadChildMsg : function(){
this.ChildMsg="Child";
console.log(this.ChildMsg);
}
}
})

var myVue = new Vue({
el: '#myVue',
data: {
ParentData:{ParentMsg: "gloubiboulga"}
}
})


And the js fiddle http://jsfiddle.net/KwakawK/hfj1tv4n/3/










share|improve this question





























    1















    So here is my problem:



    I want to make a component that takes it's values from v-with="values" and add them to my component model after some modification, then display those modified properties.



    But from what I understand, when I set values with "v-with", component data are erased so the binding between my component data (not v-with one) and my directives are lost.



    I'm really new to this framework, I don't see any solution, so I guess it was time to ask my first question here !



    Here is the HTML:



    <script type="text/x-template" id="my-template">
    <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
    <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
    </script>

    <div id="myVue">
    <my-component v-with="ParentData" ></my-component>
    </div>


    And here is the Javascript:



    Vue.component('my-component', {
    template: '#my-template',
    data: function () {
    return {
    ChildMsg: "wololo"
    }
    },
    methods:{
    reloadParentMsg : function(){
    this.ParentMsg="Parent";
    console.log(this.ParentMsg);
    },
    reloadChildMsg : function(){
    this.ChildMsg="Child";
    console.log(this.ChildMsg);
    }
    }
    })

    var myVue = new Vue({
    el: '#myVue',
    data: {
    ParentData:{ParentMsg: "gloubiboulga"}
    }
    })


    And the js fiddle http://jsfiddle.net/KwakawK/hfj1tv4n/3/










    share|improve this question



























      1












      1








      1








      So here is my problem:



      I want to make a component that takes it's values from v-with="values" and add them to my component model after some modification, then display those modified properties.



      But from what I understand, when I set values with "v-with", component data are erased so the binding between my component data (not v-with one) and my directives are lost.



      I'm really new to this framework, I don't see any solution, so I guess it was time to ask my first question here !



      Here is the HTML:



      <script type="text/x-template" id="my-template">
      <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
      <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
      </script>

      <div id="myVue">
      <my-component v-with="ParentData" ></my-component>
      </div>


      And here is the Javascript:



      Vue.component('my-component', {
      template: '#my-template',
      data: function () {
      return {
      ChildMsg: "wololo"
      }
      },
      methods:{
      reloadParentMsg : function(){
      this.ParentMsg="Parent";
      console.log(this.ParentMsg);
      },
      reloadChildMsg : function(){
      this.ChildMsg="Child";
      console.log(this.ChildMsg);
      }
      }
      })

      var myVue = new Vue({
      el: '#myVue',
      data: {
      ParentData:{ParentMsg: "gloubiboulga"}
      }
      })


      And the js fiddle http://jsfiddle.net/KwakawK/hfj1tv4n/3/










      share|improve this question
















      So here is my problem:



      I want to make a component that takes it's values from v-with="values" and add them to my component model after some modification, then display those modified properties.



      But from what I understand, when I set values with "v-with", component data are erased so the binding between my component data (not v-with one) and my directives are lost.



      I'm really new to this framework, I don't see any solution, so I guess it was time to ask my first question here !



      Here is the HTML:



      <script type="text/x-template" id="my-template">
      <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
      <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
      </script>

      <div id="myVue">
      <my-component v-with="ParentData" ></my-component>
      </div>


      And here is the Javascript:



      Vue.component('my-component', {
      template: '#my-template',
      data: function () {
      return {
      ChildMsg: "wololo"
      }
      },
      methods:{
      reloadParentMsg : function(){
      this.ParentMsg="Parent";
      console.log(this.ParentMsg);
      },
      reloadChildMsg : function(){
      this.ChildMsg="Child";
      console.log(this.ChildMsg);
      }
      }
      })

      var myVue = new Vue({
      el: '#myVue',
      data: {
      ParentData:{ParentMsg: "gloubiboulga"}
      }
      })


      And the js fiddle http://jsfiddle.net/KwakawK/hfj1tv4n/3/







      javascript vue.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 7:10









      Fisch

      3,47512238




      3,47512238










      asked Dec 8 '14 at 12:48









      KwakawKKwakawK

      614




      614
























          1 Answer
          1






          active

          oldest

          votes


















          2














          I'm not totally clear on what you're trying to do, but I believe it can be solved by using the second form of v-with, which is v-with="childProp: parentProp". Rather than the parent property overriding all of the child data, this will replace only the property on the left of the colon.



          So I think your code can be fixed by changing the v-with to this:



          <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>



          Here's the updated code as a snippet:






          // register the grid component
          Vue.component('my-component', {
          template: '#my-template',
          data: function () {
          return {
          ChildMsg: "wololo"
          }
          },
          methods:{
          reloadParentMsg : function(){
          this.ParentMsg="Parent";
          console.log(this.ParentMsg);
          },
          reloadChildMsg : function(){
          this.ChildMsg="Child";
          console.log(this.ChildMsg);
          }
          }
          })

          // bootstrap the demo
          var myVue = new Vue({
          el: '#myVue',
          data: {
          ParentData:{ParentMsg: "gloubiboulga"}
          }
          })

          <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.11.4/vue.min.js"></script>
          <script type="text/x-template" id="my-template">
          <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
          <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
          </script>

          <div id="myVue">
          <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>
          </div>





          See the Vue guide for more information.






          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%2f27358411%2fhow-to-keep-data-in-component-when-using-v-with-in-vue-js%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









            2














            I'm not totally clear on what you're trying to do, but I believe it can be solved by using the second form of v-with, which is v-with="childProp: parentProp". Rather than the parent property overriding all of the child data, this will replace only the property on the left of the colon.



            So I think your code can be fixed by changing the v-with to this:



            <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>



            Here's the updated code as a snippet:






            // register the grid component
            Vue.component('my-component', {
            template: '#my-template',
            data: function () {
            return {
            ChildMsg: "wololo"
            }
            },
            methods:{
            reloadParentMsg : function(){
            this.ParentMsg="Parent";
            console.log(this.ParentMsg);
            },
            reloadChildMsg : function(){
            this.ChildMsg="Child";
            console.log(this.ChildMsg);
            }
            }
            })

            // bootstrap the demo
            var myVue = new Vue({
            el: '#myVue',
            data: {
            ParentData:{ParentMsg: "gloubiboulga"}
            }
            })

            <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.11.4/vue.min.js"></script>
            <script type="text/x-template" id="my-template">
            <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
            <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
            </script>

            <div id="myVue">
            <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>
            </div>





            See the Vue guide for more information.






            share|improve this answer




























              2














              I'm not totally clear on what you're trying to do, but I believe it can be solved by using the second form of v-with, which is v-with="childProp: parentProp". Rather than the parent property overriding all of the child data, this will replace only the property on the left of the colon.



              So I think your code can be fixed by changing the v-with to this:



              <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>



              Here's the updated code as a snippet:






              // register the grid component
              Vue.component('my-component', {
              template: '#my-template',
              data: function () {
              return {
              ChildMsg: "wololo"
              }
              },
              methods:{
              reloadParentMsg : function(){
              this.ParentMsg="Parent";
              console.log(this.ParentMsg);
              },
              reloadChildMsg : function(){
              this.ChildMsg="Child";
              console.log(this.ChildMsg);
              }
              }
              })

              // bootstrap the demo
              var myVue = new Vue({
              el: '#myVue',
              data: {
              ParentData:{ParentMsg: "gloubiboulga"}
              }
              })

              <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.11.4/vue.min.js"></script>
              <script type="text/x-template" id="my-template">
              <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
              <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
              </script>

              <div id="myVue">
              <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>
              </div>





              See the Vue guide for more information.






              share|improve this answer


























                2












                2








                2







                I'm not totally clear on what you're trying to do, but I believe it can be solved by using the second form of v-with, which is v-with="childProp: parentProp". Rather than the parent property overriding all of the child data, this will replace only the property on the left of the colon.



                So I think your code can be fixed by changing the v-with to this:



                <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>



                Here's the updated code as a snippet:






                // register the grid component
                Vue.component('my-component', {
                template: '#my-template',
                data: function () {
                return {
                ChildMsg: "wololo"
                }
                },
                methods:{
                reloadParentMsg : function(){
                this.ParentMsg="Parent";
                console.log(this.ParentMsg);
                },
                reloadChildMsg : function(){
                this.ChildMsg="Child";
                console.log(this.ChildMsg);
                }
                }
                })

                // bootstrap the demo
                var myVue = new Vue({
                el: '#myVue',
                data: {
                ParentData:{ParentMsg: "gloubiboulga"}
                }
                })

                <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.11.4/vue.min.js"></script>
                <script type="text/x-template" id="my-template">
                <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
                <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
                </script>

                <div id="myVue">
                <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>
                </div>





                See the Vue guide for more information.






                share|improve this answer













                I'm not totally clear on what you're trying to do, but I believe it can be solved by using the second form of v-with, which is v-with="childProp: parentProp". Rather than the parent property overriding all of the child data, this will replace only the property on the left of the colon.



                So I think your code can be fixed by changing the v-with to this:



                <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>



                Here's the updated code as a snippet:






                // register the grid component
                Vue.component('my-component', {
                template: '#my-template',
                data: function () {
                return {
                ChildMsg: "wololo"
                }
                },
                methods:{
                reloadParentMsg : function(){
                this.ParentMsg="Parent";
                console.log(this.ParentMsg);
                },
                reloadChildMsg : function(){
                this.ChildMsg="Child";
                console.log(this.ChildMsg);
                }
                }
                })

                // bootstrap the demo
                var myVue = new Vue({
                el: '#myVue',
                data: {
                ParentData:{ParentMsg: "gloubiboulga"}
                }
                })

                <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.11.4/vue.min.js"></script>
                <script type="text/x-template" id="my-template">
                <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
                <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
                </script>

                <div id="myVue">
                <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>
                </div>





                See the Vue guide for more information.






                // register the grid component
                Vue.component('my-component', {
                template: '#my-template',
                data: function () {
                return {
                ChildMsg: "wololo"
                }
                },
                methods:{
                reloadParentMsg : function(){
                this.ParentMsg="Parent";
                console.log(this.ParentMsg);
                },
                reloadChildMsg : function(){
                this.ChildMsg="Child";
                console.log(this.ChildMsg);
                }
                }
                })

                // bootstrap the demo
                var myVue = new Vue({
                el: '#myVue',
                data: {
                ParentData:{ParentMsg: "gloubiboulga"}
                }
                })

                <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.11.4/vue.min.js"></script>
                <script type="text/x-template" id="my-template">
                <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
                <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
                </script>

                <div id="myVue">
                <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>
                </div>





                // register the grid component
                Vue.component('my-component', {
                template: '#my-template',
                data: function () {
                return {
                ChildMsg: "wololo"
                }
                },
                methods:{
                reloadParentMsg : function(){
                this.ParentMsg="Parent";
                console.log(this.ParentMsg);
                },
                reloadChildMsg : function(){
                this.ChildMsg="Child";
                console.log(this.ChildMsg);
                }
                }
                })

                // bootstrap the demo
                var myVue = new Vue({
                el: '#myVue',
                data: {
                ParentData:{ParentMsg: "gloubiboulga"}
                }
                })

                <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.11.4/vue.min.js"></script>
                <script type="text/x-template" id="my-template">
                <p v-on="click:reloadParentMsg">Msg parent : {{ParentMsg}}</p>
                <p v-on="click:reloadChildMsg">Msg child : {{ChildMsg}}</p>
                </script>

                <div id="myVue">
                <my-component v-with="ParentMsg: ParentData.ParentMsg" ></my-component>
                </div>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 8 '15 at 4:10









                MiguelMiguel

                6,799125089




                6,799125089
































                    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%2f27358411%2fhow-to-keep-data-in-component-when-using-v-with-in-vue-js%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

                    'app-layout' is not a known element: how to share Component with different Modules

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    WPF add header to Image with URL pettitions [duplicate]