Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app1">
  vm.$localStorage.num: {{ $localStorage.num }}
  <button @click="add">+1</button>
  <hr />
  Object.keys(vm.$localStorage):
  <ul>
    <li v-for="(v, k) in this.$localStorage">{{ k }}</li>
  </ul>
  <button @click="addFoobar">foobar = 100</button>
  <button @click="deleteFoobar">delete foobar</button>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                class LocalStorage {
  static install (Vue) {
    const instance = new LocalStorage(Vue)
    
    Object.defineProperty(Vue.prototype, '$localStorage', {
      get: () => instance.storage,
    })

    Vue.localStorage = instance.storage
    
    return instance
  }
  
  static pack (value) {
    if (value === undefined)
      return undefined
    
    return JSON.stringify(value)
  }
  static unpack (value) {
    if (value === undefined)
      return undefined

    try {
      return JSON.parse(value)
    } catch (e) {
      return value
    }
  }
  
  static createCache() {
    return new Proxy({}, {
      get(target, property, receiver) {
        if (!(property in target)) {
          target[property] = LocalStorage.unpack(window.localStorage.getItem(property))
        }
        
        return target[property]
      },
      set(target, property, value, receiver) {
        target[property] = value
        if (value === undefined) {
          window.localStorage.removeItem(property)
        } else {
          window.localStorage.setItem(property, LocalStorage.pack(value))
        }
        
        return true
      },
      deleteProperty(target, property) {
        window.localStorage.removeItem(property)
        delete target[property]
        
        return true
      },
    })
  }

  constructor (Vue) {
    const _cache = LocalStorage.createCache()
    
    const observeItem = (target, property) => {
      Object.defineProperty(target, property, {
        get: () => _cache[property],
        set: (val) => _cache[property] = val,
        configurable: true,
      })
    
      Vue.util.defineReactive(target, property, target[property])
    }
    
    this.storage = new Proxy({}, {
      get(target, property, receiver) {
        if (!target.hasOwnProperty(property) && typeof property === 'string') {
          observeItem(target, property)
        }
        return target[property]
      },
      set(target, property, value) {
        if (!target.hasOwnProperty(property) && typeof property === 'string') {
          observeItem(target, property)
        }
        
        target[property] = value
        return true
      },
      deleteProperty(target, property) {
        delete _cache[property]
        target[property] = undefined
        
        return true
      },
      ownKeys(target) {
        return Reflect.ownKeys(window.localStorage)
      },
      getOwnPropertyDescriptor(target, property) {
        return Reflect.getOwnPropertyDescriptor(window.localStorage, property)
      },
      has(target, property) {
        return property in window.localStorage
      },
    })
    
    window.addEventListener('storage', ({key, newValue}) => {
      if (key in _cache) {
        if (newValue === null) {
          delete this.storage[key]
        } else {
          this.storage[key] = LocalStorage.unpack(newValue)
        }
      }
    })
  }
}

Vue.use(LocalStorage)

if (Vue.localStorage.num === undefined) {
  Vue.localStorage.num = 0
}

new Vue({
  el: '#app1',
  methods: {
    add() {
      this.$localStorage.num = (this.$localStorage.num || 0) + 1
    },
    addFoobar() {
      this.$localStorage.foobar = 100
    },
    deleteFoobar() {
      delete this.$localStorage.foobar
    },
  },
})
              
            
!
999px

Console