如何在 VUE 中.js将此表达式 **newCat = $event.target.value** 从 **input



How in VUE.js 将此表达式 newCat = $event.target.value 从输入转移到方法,在输入中只留下 newCat然后在 vuex 中使用它。像这样@input="新猫">

    <f7-list form>
        <f7-list-input
          :value="newCat"
          @input="newCat = $event.target.value"
          type="text"
          placeholder="some item"
        ></f7-list-input>
        <f7-button fill color="blue" @click="addCat">Add some</f7-button>
    </f7-list>

data() {
    return{
        cats:[],
        newCat: null
  }
},
mounted() {
  if (localStorage.getItem('cats')) {
    try {
      this.cats = JSON.parse(localStorage.getItem('cats'));
    } catch(e) {
      localStorage.removeItem('cats');
    }
  }
},
methods: {
  addCat() {
    if (!this.newCat) {
      return;
    }
    this.cats.push(this.newCat);
    this.newCat = '';
    this.saveCats();
  },
  removeCat(x) {
    this.cats.splice(x, 1);
    this.saveCats();
  },
  saveCats() {
    const parsed = JSON.stringify(this.cats);
    localStorage.setItem('cats', parsed);
  }
}
}

试试这个:

<template>
  <f7-list form>
      <f7-list-input
        :value="newCat"
        @input="newCatOnInput"
        type="text"
        placeholder="some item"
      ></f7-list-input>
      <f7-button fill color="blue" @click="addCat">Add some</f7-button>
  </f7-list>
</template>
<script>
export default {
  data() {
    return{
      cats:[],
      newCat: null
    };
  },
  mounted() {
    if (localStorage.getItem('cats')) {
      try {
        this.cats = JSON.parse(localStorage.getItem('cats'));
      } catch(e) {
        localStorage.removeItem('cats');
      }
    }
  },
  methods: {
    addCat() {
      if (!this.newCat) {
        return;
      }
      this.cats.push(this.newCat);
      this.newCat = '';
      this.saveCats();
    },
    removeCat(x) {
      this.cats.splice(x, 1);
      this.saveCats();
    },
    saveCats() {
      const parsed = JSON.stringify(this.cats);
      localStorage.setItem('cats', parsed);
    },
    newCatOnInput(e) {
      this.newCat = e.target.value;
    }
  }
}
</script>

最新更新