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

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!

主站蜘蛛池模板: 红原县| 元阳县| 花莲市| 蓬溪县| 和龙市| 元朗区| 霸州市| 新巴尔虎右旗| 龙南县| 同江市| 望谟县| 新巴尔虎左旗| 贵阳市| 顺昌县| 元朗区| 沈丘县| 阿鲁科尔沁旗| 临潭县| 灵宝市| 汶川县| 正安县| 满洲里市| 金川县| 乐至县| 龙门县| 勐海县| 济宁市| 祁连县| 宁陵县| 娱乐| 汕头市| 建水县| 达日县| 长岭县| 樟树市| 林周县| 招远市| 阳东县| 咸宁市| 盱眙县| 嘉善县|