国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解vuex的簡單todolist例子

2024-05-06 15:36:58
字體:
來源:轉載
供稿:網友

一個簡單的vuex應用的小例子,一段自己的學習記錄。

todolist就是一個簡單的輸入框,一個按鈕,一個文本顯示區域,可以逐條進行刪除。

1.在用vue-cli生成好的HelloWorld.vue文件中直接寫代碼,先刪除所有的自帶代碼

<template> <div class="hello">  <input type="text">  <button>增加事項</button>  <ul>   <li>item</li>  </ul> </div></template>

要把`input`中的值在經過`button`點擊后,顯示在`li`中,`input`有`v-model`屬性進行值的綁定,

讓`li`的數據是一個數組。相當于在數組中push input的值。

2.在src目錄下,新建一個store文件夾,創建一個index.js文件

import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)const store = new Vuex.Store({ state: {  inputVal: 'lily',  list: ['1', '2', '3'] }, mutations: {  changeListValue(state, inputVal) {   state.list.push(inputVal)   state.inputVal = ''  },  handleDel(state, idx) {   state.list.splice(idx, 1)  } }, actions: {  changeListValue: ({commit}, inputVal) => {   return commit('changeListValue', inputVal)  },  handleDel: ({commit}, idx) => {   return commit('handleDel', idx)  } }})export default store

3.回到HelloWorld.vue

<template> <div class="hello">  <input v-model="$store.state.inputVal" type="text">  <button @click="changeListValue(inputVal)">增加事項</button>  <ul v-for="(item, idx) in list">   <li @click="handleDel(idx)">{{item}}</li>  </ul> </div></template><script> import {mapState, mapActions} from 'vuex' export default {  name: 'HelloWorld',  computed: {   ...mapState(['list', 'inputVal'])  },  methods: {   ...mapActions(['changeListValue', 'handleDel'])  } }</script>

4.完成以后,有個困擾就是在input的v-model中寫inputVal會報錯,請大神幫我解答下。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 双鸭山市| 怀集县| 潼南县| 上思县| 洞口县| 化州市| 乌拉特中旗| 德安县| 黎川县| 吴江市| 长兴县| 海宁市| 睢宁县| 繁峙县| 上高县| 麻城市| 宕昌县| 专栏| 江城| 比如县| 巴马| 禄丰县| 玛沁县| 襄城县| 陇川县| 县级市| 航空| 祁连县| 佛冈县| 长沙市| 且末县| 岑巩县| 晴隆县| 广丰县| 隆回县| 汉川市| 西乌| 定兴县| 昌宁县| 恩施市| 静宁县|