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

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',
},
主站蜘蛛池模板: 鱼台县| 衡阳县| 五华县| 合作市| 宣武区| 芦山县| 重庆市| 师宗县| 延吉市| 浠水县| 安新县| 将乐县| 大足县| 罗甸县| 阳山县| 朝阳区| 东明县| 疏勒县| 济宁市| 株洲市| 鄢陵县| 肇州县| 江孜县| 长寿区| 苏尼特右旗| 宾阳县| 广德县| 宁明县| 休宁县| 将乐县| 莱西市| 康保县| 方城县| 柳林县| 清原| 桦川县| 浠水县| 南丰县| 恩平市| 巨鹿县| 兴安盟|