How to reference data variable from another data v

2020-05-30 03:58发布

I have this in vue data:

data() {
    return {

      names: [],
      length: names.length,
}

But this does not work as RefereneError ( names is undefined ) is thrown. I used this.names but makes no difference.

标签: vue.js
1条回答
Bombasti
2楼-- · 2020-05-30 04:16

You need to do something like this to make it work:

1st way

data() {
    let defaultNames = [];
    return {
      names: defaultNames,
      length: defaultNames.length,
}

2nd way — using computed data (the best way):

data() {
    return {
      names: [],
    }
},
computed: {
    length() {
        return this.names.length;
    }
}
查看更多
登录 后发表回答