Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
953 views
in Technique[技术] by (71.8m points)

element-ui的el-input从vuex的state中获取value值后,输入框无法编辑?

我想在el-input框中输入值,然后监听变化并传到state中,一开始使用的是@change,但是没有变化。看了文档后改用input,但是无法改变文本框的内容,代码如下:

<el-input placeholder="请输入内容" class="td-input" :value="inputValue" @input.native="handleInputChange"></el-input>
export default {
  name: 'app',
  data () {
    return {}
  },
  created () {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['list', 'inputValue'])
  },
  methods: {
    handleInputChange (e) {
      console.log(e.target.value)
    }
  }
}

value值是绑定在vuex的的state中的

export default new Vuex.Store({
  state: {
    list: [],
    inputValue: 'test'
  },
  mutations: {
    initList (state, list) {
      state.list = list
    },
    setInputValue (state, val) {
      state.inputValue = val
    }
  },
  actions: {
    getList (context) {
      axios.get('/list.json').then(({ data }) => {
        context.commit('initList', data)
      })
    }
  },
  modules: {
  }
})

输入数字的时候console.log还是原来的内容,但是输入输入拼音的时候可以监听到变化。
页面截图
image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

因为你没有改变state中inputValue的值呀

handleInputChange (e) {
      console.log(e.target.value)
    this.$store.commit('setInputValue',e.target.value)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...