1

So I have this set-up, which crashes on v-for construct of table-component. It shows an error: "Property or method "tablesList" is not defined on the instance but referenced during render". If I omit v-for table-component renders. If I access this data from "container" component all is fine. So the problem is in accessing data from child template in parent template.

What am I doing wrong?

let container = Vue.component("container", {
        props: ["item"],
        template: `<div class="container">

            <table-component v-for="item in tablesList"></table-component>
          </div>`
      });

let table = Vue.component("table-component", {
        props: ["item"],
        template: `<div class="table">
            this is a table
          </div>`
      });

      let app = new Vue({
        el: "#app",
        data() {
          return {
            containersList: [],
            tablesList: [{item:'item'}]
          };
        },
        methods: {
          anyMethod() {

          }
        }
      });
    </script>
2
  • Why dont you add tablesList to your container component instead of table-component? Commented Mar 30, 2020 at 16:20
  • tablesList is in Vue instance not in table-component. Or am I getting you wrong? Commented Mar 30, 2020 at 17:35

2 Answers 2

1

You are using tablesList in container component But you defined it in app.

You need to add tablesList in container like below,

let container = Vue.component("container", {
    props: ["item"],
    data: () => {
       return {
          tablesList: [{item:'item'}]
       }
    },
    template: `<div class="container">
                <table-component v-for="item in tablesList"></table-component>
              </div>`
    });

NOTE: Use v-bind:key when use v-for.

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

2 Comments

aha, that makes sense. Let me check it out. Yet upvote for you :)
Alright! It worked. I get a bunch of new errors, but I'll figure them out later. Thanks mate! ))
1

You need to define tablesList in props => https://v2.vuejs.org/v2/guide/components.html#Passing-Data-to-Child-Components-with-Props

1 Comment

Could you explain how to do it, please?

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.