官术网_书友最值得收藏!

Proxied properties

You may have noticed that ourescapeKeyListenermethod can refer tothis.modalOpen. Shouldn't it bethis.methods.modalOpen?

When a Vue instance is constructed, it proxies any data properties, methods, and computed properties to the instance object. This means that from within any method you can refer tothis.myDataProperty,this.myMethod, and so on, rather thanthis.data.myDataPropertyorthis.methods.myMethod,as you might assume:

var app = new Vue({
  data: { 
    myDataProperty: 'Hello'
  },
  methods: {
    myMethod: function() {
      return this.myDataProperty + ' World';
    }
  }
});

console.log(app.myMethod());
// Output: 'Hello World'

You can see these proxied properties by printing the Vue object in the browser console:

Figure 2.12. Our app's Vue instance

Now the simplicity of text interpolations might make more sense, they have the context of the Vue instance, and thanks to proxied properties, can be referenced like{{ myDataProperty }}.

However, while proxying to the root makes syntax terser, a consequence is that you can't name your data properties, methods, or computed properties with the same name!

主站蜘蛛池模板: 专栏| 茶陵县| 永顺县| 石楼县| 江山市| 修武县| 驻马店市| 容城县| 卢氏县| 古丈县| 西安市| 孟村| 抚顺县| 阿城市| 全州县| 仙游县| 呼伦贝尔市| 宜春市| 如东县| 盐山县| 紫阳县| 阳东县| 河南省| 阜宁县| 芷江| 衢州市| 丰原市| 溆浦县| 连南| 沅陵县| 汕尾市| 静海县| 永州市| 通道| 宁津县| 湄潭县| 任丘市| 浦县| 达日县| 伊春市| 福安市|