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

Using a method

There is a good coding principle that says Don't Repeat Yourself, and we really should follow it. That's why we can write some logic in reusable functions called methods. Let's move our saving logic into one:

  1. Add a new methods option to the Vue instance and use the localStorage API there:
      new Vue({
        // ...

        methods: {
          saveNote (val) {
            console.log('saving note:', val)
            localStorage.setItem('content', val)
          },
        },
      })
  1. We can now use the method name in the handler option of our watcher:
      watch: {
        content: {
          handler: 'saveNote',
        },
      },

Alternatively, we can use it with the shorter syntax:

watch: {
  content: 'saveNote',
},
主站蜘蛛池模板: 衢州市| 黔西| 四平市| 金川县| 保靖县| 威海市| 阳高县| 彰化县| 嘉祥县| 玉环县| 南木林县| 惠东县| 乌苏市| 揭阳市| 衢州市| 双桥区| 龙胜| 阳新县| 砚山县| 兴隆县| 庆安县| 海南省| 达日县| 微山县| 吉安县| 沾益县| 女性| 洛阳市| 汨罗市| 正安县| 定西市| 壶关县| 昆明市| 开远市| 揭东县| 西峡县| 双峰县| 林口县| 北辰区| 家居| 墨脱县|