1

I have a header component. i cant able to update value of header component on button click

   <!DOCTYPE html>
     <html> 
      <head>
       <title>Page Title</title>
         </head>
        <body>
         <header>
         <my-component ref="foo"></my-component>
        </header>
           <div id="app">
           <h1>Component Test</h1>
          <button v-on:click="test">Button</button>
          </div>
     <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    var MyComponent = Vue.extend({
    template: '<div><p>Hello</p><div v-if="islogin">User</div></div>',
    data: function () {
      return {
        islogin: false
      };
    }
   });

var vm = new Vue({
  el: '#app',
  components: {
    'my-component': MyComponent
  },
  methods: {
    test: function () {         
      this.$refs.foo.islogin = true;
    }
  }
});


 </script>
</body>
</html>

i want to update the islogin to true on button click. Now it shows "TypeError: Cannot set property 'islogin' of undefined" error.

Thanks

2
  • Is there any reason why you use ref? Otherwise I would suggest to use data binding instead... Commented Apr 23, 2019 at 9:43
  • 1
    no reason for using href. my-component is common and its used every pages. From each page i want to update the "islogin" value. Commented Apr 23, 2019 at 11:13

2 Answers 2

1

Your component 'my-component' is not in the scope of Vuejs. Vue is in the div with the id 'app'. So you can't add others components outside this main div

Sign up to request clarification or add additional context in comments.

Comments

1

Put the component inside Vuejs #app scope :

       <div id="app">
          <header>
             <my-component ref="foo"></my-component>
          </header>
          <h1>Component Test</h1>
          <button v-on:click="test">Button</button>
       </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.