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

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!

主站蜘蛛池模板: 黎平县| 铁岭县| 林甸县| 巩义市| 沁源县| 马龙县| 厦门市| 卢龙县| 射阳县| 渝北区| 西充县| 甘南县| 南京市| 恩施市| 金塔县| 嘉义县| 调兵山市| 丰城市| 屯昌县| 丰顺县| 沾益县| 城固县| 富宁县| 桐梓县| 肇庆市| 永顺县| 湟中县| 合作市| 威信县| 宜春市| 铜陵市| 阜阳市| 崇州市| 巨野县| 嫩江县| 托里县| 思茅市| 本溪市| 湟源县| 连云港市| 手游|