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

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!

主站蜘蛛池模板: 临湘市| 汝阳县| 招远市| 云浮市| 穆棱市| 望谟县| 奉新县| 吴忠市| 万安县| 鄂托克旗| 克拉玛依市| 关岭| 醴陵市| 荥经县| 芜湖县| 木里| 武清区| 将乐县| 罗定市| 余庆县| 龙胜| 贡山| 林州市| 临潭县| 灌阳县| 舞阳县| 许昌县| 苍溪县| 克拉玛依市| 阿瓦提县| 保德县| 林甸县| 桓仁| 马公市| 林芝县| 汉源县| 高阳县| 西安市| 定南县| 五常市| 苗栗县|