vue中的一个疑问.

"Hello World, Hello Blog"

Posted by wudimingwo on December 15, 2018
1
2
3
4
5
6
7
8
var app = new Vue({
  el : '#app',
  data : {
    arr : [1,2,3],
    num : arr[1] ******* 这里是失败的.我的问题是,data里想要引用data里另一个变量要怎么做?
  }

})

data 里的属性都会被添加到实例上, 也就是 app 上. image.png image.png 所以我这么写了一下

1
2
3
4
5
6
7
8
var app = new Vue({
             el : '#app',
             data : {
               arr : [1,2],
               num : app.arr[0]
             }
           });
           console.log(app);

image.png 报错,这我就很丢人, 很明显这里不能用app 应该用 this

1
2
3
4
5
6
7
8
var app = new Vue({
             el : '#app',
             data : {
               arr : [1,2],
               num : this.arr[0]
             }
           });
           console.log(app);

结果 image.png 还是报错,错误说,没有arr这个属性. 我只能理解为, data这个属性没有被”解析完”时,是无法在实例上访问的. 所以data里访问data里其他属性应该是不行的.

而像下面这种

1
2
3
4
5
6
7
8
9
10
11
12
var app = new Vue({
             el : '#app',
             data : {
               arr : [1,2],
             },
             computed : {
               num : function () {
               	return this.arr[1]
               }
             }
           });
           console.log(app);

image.png 也就是说,data里的数据是可以被其他属性所访问, 而data不能访问自己的,也不能访问其他属性的(比如computed)?

问题在于, 如果我就想在data里访问data里的其他属性怎么弄?