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

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',
},
主站蜘蛛池模板: 宣威市| 绥滨县| 罗山县| 西和县| 崇信县| 南漳县| 龙海市| 卢湾区| 玛曲县| 新巴尔虎左旗| 锡林浩特市| 敦煌市| 淮南市| 唐海县| 北流市| 历史| 海丰县| 沂源县| 鲁甸县| 隆林| 古田县| 北流市| 余庆县| 中宁县| 黑龙江省| 济源市| 巴青县| 静宁县| 舟曲县| 英吉沙县| 囊谦县| 策勒县| 天长市| 宜春市| 松江区| 鹿泉市| 策勒县| 常宁市| 建阳市| 花莲县| 灵台县|