如何在 Vue.js 中将输入保存到本地存储中的输入
How in Vue.js save input to input in localstorage
例如,我从官方 Vue.js 站点做所有事情。但我需要使用框架 7。在输入中我显示了 [object InputEvent]。当我尝试写一些文本时,也会显示 [object InputEvent]。
如何将名称保存在本地存储中并在输入中显示出来?
PS 框架 7 中的 v-model 不工作
<f7-list form>
<f7-list-input
label="Username"
name="username"
placeholder="Username"
type="text"
v-bind:value="name"
required validate
pattern="[3-9a-zA-Zа-яА-ЯёЁ]+"
@input="persist"
/>
</f7-list>
<script>
export default {
data() {
return{
name: '',
}
},
mounted() {
if (localStorage.name) {
this.name = localStorage.name;
}
},
methods: {
persist(){
name=$event.target.value;
localStorage.name = this.name;
}
}
};
</script>
that's what output to input
简单:
localStorage.setItem('name', this.name)
this.name = localStorage.getItem('name')
使用正确的 localstorage 方法更新了代码并删除了导致您的问题的一行
替换
name=$event.target.value;
和
this.name = $event.target.value;
请在下面找到包含更新方法和重构代码的更新代码。
<f7-list form>
<f7-list-input
label="Username"
name="username"
placeholder="Username"
type="text"
v-bind:value="name"
required validate
pattern="[3-9a-zA-Zа-яА-ЯёЁ]+"
@input="persist"
/>
</f7-list>
<script>
export default {
data() {
return{
name: '',
}
},
mounted() {
if (localStorage.name) {
//retrive name from localstorage here.
this.name = localStorage.getItem('name')
}
},
methods: {
persist(){
/* set name to localstorage here
using setItem is recommended way of doing but even without that yourcode should work.*/
localStorage.setItem('name', $event.target.value)
}
}
};
</script>
例如,我从官方 Vue.js 站点做所有事情。但我需要使用框架 7。在输入中我显示了 [object InputEvent]。当我尝试写一些文本时,也会显示 [object InputEvent]。
如何将名称保存在本地存储中并在输入中显示出来?
PS 框架 7 中的 v-model 不工作
<f7-list form>
<f7-list-input
label="Username"
name="username"
placeholder="Username"
type="text"
v-bind:value="name"
required validate
pattern="[3-9a-zA-Zа-яА-ЯёЁ]+"
@input="persist"
/>
</f7-list>
<script>
export default {
data() {
return{
name: '',
}
},
mounted() {
if (localStorage.name) {
this.name = localStorage.name;
}
},
methods: {
persist(){
name=$event.target.value;
localStorage.name = this.name;
}
}
};
</script>
that's what output to input
简单:
localStorage.setItem('name', this.name)
this.name = localStorage.getItem('name')
使用正确的 localstorage 方法更新了代码并删除了导致您的问题的一行
替换
name=$event.target.value;
和
this.name = $event.target.value;
请在下面找到包含更新方法和重构代码的更新代码。
<f7-list form>
<f7-list-input
label="Username"
name="username"
placeholder="Username"
type="text"
v-bind:value="name"
required validate
pattern="[3-9a-zA-Zа-яА-ЯёЁ]+"
@input="persist"
/>
</f7-list>
<script>
export default {
data() {
return{
name: '',
}
},
mounted() {
if (localStorage.name) {
//retrive name from localstorage here.
this.name = localStorage.getItem('name')
}
},
methods: {
persist(){
/* set name to localstorage here
using setItem is recommended way of doing but even without that yourcode should work.*/
localStorage.setItem('name', $event.target.value)
}
}
};
</script>