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

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',
},
主站蜘蛛池模板: 治多县| 阳曲县| 西畴县| 鄂尔多斯市| 五寨县| 乌拉特前旗| 子长县| 家居| 博野县| 成武县| 汉川市| 任丘市| 洱源县| 石嘴山市| 林芝县| 竹山县| 宁武县| 平武县| 玉山县| 乌海市| 洱源县| 勃利县| 邮箱| 上饶市| 高淳县| 射洪县| 玉龙| 桦川县| 沅陵县| 景泰县| 临沂市| 云浮市| 华坪县| 体育| 铅山县| 汶上县| 临沭县| 南城县| 英德市| 同江市| 浮山县|