【问题标题】:Vuejs: data list not accessible from template in another templateVuejs:无法从另一个模板中的模板访问数据列表
【发布时间】:2020-07-11 01:47:11
【问题描述】:

所以我有这个设置,它在表组件的 v-for 构造上崩溃。它显示一个错误:“属性或方法“tablesList”未在实例上定义,但在渲染期间被引用”。如果我省略 v-for table-component 渲染。如果我从“容器”组件访问这些数据,一切都很好。所以问题在于从父模板中的子模板访问数据。

我做错了什么?

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>

【问题讨论】:

  • 为什么不将tablesList 添加到container 组件而不是table-component?
  • tablesList 在 Vue 实例中而不是在表组件中。还是我误会了?

标签: javascript vue.js


【解决方案1】:

你在container component中使用tablesList但是你在app中定义了它。

您需要在container 中添加tablesList,如下所示,

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>`
    });

注意:在使用v-for时使用v-bind:key。

【讨论】:

  • 啊哈,这很有道理。让我检查一下。然而为你投票:)
  • 好的!有效。我得到了一堆新的错误,但我稍后会解决它们。谢了哥们! ))
【解决方案2】:

【讨论】:

  • 你能解释一下怎么做吗?
猜你喜欢
  • 2017-06-23
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
相关资源
最近更新 更多