export default ({ app }, inject) => { inject("localStorage", { set(key = "", val = {}) { localStorage.setItem(key, JSON.stringify(val)); }, get(key = "") { const obj = JSON.parse(localStorage.getItem(key)); // 避免傳回 null if (!obj) { return {}; } return obj; }, remove(key = "") { localStorage.removeItem(key); }, removeAll() { localStorage.clear(); } }); };
plugins: [ "~/plugins/hello.js", "~/plugins/axios.js", "~/plugins/notification.js", "~/plugins/localStorage.js" ],
mounted() { this.$hello("我在前端被mounted了"); this.$localStorage.set("userData", { name: "tad", age: 48 }); console.log('userData', this.$localStorage.get("userData")); },
進階搜尋