【问题标题】:vue component data watch outsidevue组件数据看外面
【发布时间】:2018-07-14 13:33:39
【问题描述】:

在我的应用程序中,我有一个组件,我希望在组件之外拥有他的属性。 我创建了这个例子:

Vue.component('vue-table', {
  template: '<div><template v-for="row in apiData.content"><span>{{row.name}}</span><button @click="remove(row)">remove</button><br></template></div>',
  data: function() {
    return {
    //this data will be loaded from api 
        apiData: {
        total: 20,
        content: [
            {id: 10, name: 'Test'},
            {id: 12, name: 'John'},
            {id: 13, name: 'David'},
        ],
      },
    };
  },
  methods: {
    remove(row) {
        this.apiData.content.splice(this.apiData.content.indexOf(row), 1);
    },
  },
})


new Vue({
  el: '#app',
  methods: {
  isActive(){
    //how can i check if in vue-table apiData.content > 0?
    //return this.$refs.table.apiData.data.length > 0;
  },
  },
})

http://jsfiddle.net/z11fe07p/2806/

所以我想在 vue-table apiData.content.length > 0 的长度时将 span 类更改为“活动”

我该怎么做?

【问题讨论】:

    标签: vue.js vue-component watch


    【解决方案1】:

    标准做法是在子级中发出一个事件,让父级接收并对其采取行动。您可能想知道是否可以查看数组的长度——在组件实例化时甚至不存在的数组——答案是肯定的。

    查看watch 部分。 IMO,这太酷了,以至于它可能不受欢迎。

    Vue.component('vue-table', {
      template: '<div><template v-for="row in apiData.content"><span>{{row.name}}</span><button @click="remove(row)">remove</button><br></template></div>',
      data: function() {
        return {
          //this data will be loaded from api 
          apiData: {},
        };
      },
      methods: {
        remove(row) {
          this.apiData.content.splice(this.apiData.content.indexOf(row), 1);
        },
      },
      watch: {
        'apiData.content.length': function(is, was) {
          this.$emit('content-length', is);
        }
      },
      created() {
        this.apiData = {
          total: 20,
          content: [{
            id: 10,
            name: 'Test'
          }, {
            id: 12,
            name: 'John'
          }, {
            id: 13,
            name: 'David'
          }, ],
        };
      }
    })
    
    
    new Vue({
      el: '#app',
      data: {
        isActive: false
      },
      methods: {
        setActive(contentLength) {
          this.isActive = contentLength > 0;
        }
      },
    })
    #app {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    
    .active {
      font-weight: bold;
    }
    <script src="//unpkg.com/vue@latest/dist/vue.js"></script>
    <div id="app">
      <p>
        <span :class="{active: isActive}">Users:</span>
      </p>
      <vue-table refs="table" @content-length="setActive"></vue-table>
    </div>

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 1970-01-01
      • 2019-06-12
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 2018-08-03
      • 2017-10-17
      相关资源
      最近更新 更多