Nuxt/examples/with-cookies/pages/index.vue

86 lines
1.7 KiB
Vue
Raw Normal View History

2017-08-24 16:48:31 +00:00
<template>
<div>
<h1>Cookies</h1>
<table>
<thead>
<tr>
<th>Key</th><th>Value</th><th />
2017-08-24 16:48:31 +00:00
</tr>
</thead>
<tbody>
2017-11-26 13:40:57 +00:00
<tr v-for="(value, key) in cookies" :key="key">
2017-08-24 16:48:31 +00:00
<td>{{ key }}</td>
<td>{{ value }}</td>
<td>
<button @click="removeCookie(key)">
Remove
</button>
</td>
2017-08-24 16:48:31 +00:00
</tr>
</tbody>
</table>
<h2>Add a new cookie</h2>
<form @submit.prevent="addCookie">
<input v-model="newCookie.key" type="text" placeholder="Key" class="key">:
<input v-model="newCookie.value" type="text" placeholder="Value" class="value">
<button type="submit">
Add
</button>
2017-08-24 16:48:31 +00:00
</form>
</div>
</template>
<script>
export default {
2017-08-24 18:37:54 +00:00
data: () => ({
newCookie: {
key: '',
value: ''
}
}),
computed: {
cookies () {
2017-08-24 18:37:54 +00:00
return this.$cookies.cookies
2017-08-24 16:48:31 +00:00
}
},
methods: {
addCookie () {
2017-08-24 18:37:54 +00:00
// Make sure the cookie is not empty
if (!this.newCookie.key || !this.newCookie.value) { return }
2017-08-24 18:37:54 +00:00
// Sanitize the key to avoid spaces
const cookieKey = this.newCookie.key.replace(/\s/g, '-')
// Add the cookie
this.$cookies.set(cookieKey, this.newCookie.value)
// Reset newCookie data
this.newCookie.key = this.newCookie.value = ''
2017-08-24 16:48:31 +00:00
},
removeCookie (key) {
2017-08-24 18:37:54 +00:00
this.$cookies.remove(key)
2017-08-24 16:48:31 +00:00
}
}
}
</script>
<style scoped>
table {
text-align: left;
}
th, td {
padding-right: 10px;
}
input.key {
width: 50px;
}
input.value {
width: 100px;
}
input, button {
border: 1px #ddd solid;
background: white;
padding: 5px;
}
button[type="submit"] {
margin-left: 5px;
}
</style>