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

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',
},
主站蜘蛛池模板: 东莞市| 卢氏县| 乌什县| 钟山县| 外汇| 赣榆县| 河池市| 新龙县| 普洱| 台南县| 建平县| 汾阳市| 辽阳市| 河北区| 巴林右旗| 武陟县| 宜黄县| 孝义市| 大悟县| 普定县| 武威市| 遵义县| 弥渡县| 清涧县| 伊金霍洛旗| 文登市| 贡山| 德州市| 镇安县| 庄河市| 禹城市| 华池县| 建昌县| 泰顺县| 麦盖提县| 靖江市| 盐源县| 扎赉特旗| 安泽县| 庆安县| 阳城县|