1, a new directory in the src util, the new tool in the directory module js
// only one function exposed outwardly by a function // necessarily exposed outwardly by a plurality of functional objects // define constants const = TODOS_KEY 'todos_key' Export default { saveTodos (Todos) { window.localStorage.setItem (TODOS_KEY, the JSON. the stringify (Todos)) }, readTodos () { return the JSON.parse (window.localStorage.getItem (TODOS_KEY) || '[]' ) } }
2, incorporated in this component requires js use this tool module
import storageUtil from './util/storageUtil'
3, use this tool module
Data () { return { Todos: storageUtil.readTodos () } }, Watch: { Todos: { Deep: to true , // the depth of the monitor, change the contents of the array elements will be monitored Handler: function (newValue, oldValue ) { storageUtil.saveTodos (newValue) } } }