1

I do everything for example from the official Vue.js site. But I need to use framework 7. And in input i displayed [object InputEvent]. And when I try to write some text is also displayed [object InputEvent].

How to save a name in localstorage and display it back in input?

PS v-model in framework 7 does not work

  <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

2
  • There's a typo in your script script tag that you should look at as well: persist vs pArsist, but the answer should be correct Commented Apr 9, 2019 at 19:34
  • I fix it, but still doesn't work Commented Apr 9, 2019 at 19:48

2 Answers 2

1

Updated code with proper methods of localstorage and removed a line which was cause for your issue

replace

name=$event.target.value;

with

this.name = $event.target.value;

Please find below updated code with updated methods and refactored code.

 <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>
Sign up to request clarification or add additional context in comments.

2 Comments

Same problem with [object InputEvent]. Console error --> [Vue warn]: Error in v-on handler: "ReferenceError: $event is not defined" found in ---> <F7ListInput> <F7List> <F7PageContent> <F7Page> <F7View> <Login> at src/pages/login.vue <F7View> <F7App> <App> at src/app.vue <Root> vue.esm.js:629:7
Add this in persist and all works now this.name = event.target.value;
1

Simply :

localStorage.setItem('name', this.name)

this.name = localStorage.getItem('name')

3 Comments

Did I do right? methods: { parsist(){ localStorage.setItem(key, this.name); this.name = localStorage.getItem(key); } }
@Dio those go in the separate functions
Can you please do an example how it should be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.