最近從react轉vue,嘗試限制用戶輸入,比如輸入內容只能為number,否則不改變值,在react里有受限組件所以很容易做到,但是vue里我嘗試用react的方法貌似不行。。通過正則過濾,達到了authorId只能為純數字的目的,但是在頁面上input就算輸入不是數字也會顯示嘗試用div輸出authorId,是只輸出符合要求的內容。問題:不用過濾器,怎么才能使input達到div的效果,只輸出符合要求的內容(同步視圖和數據)部分代碼template <template> <input @input="changeAuthorId" :value="authorId"> <div>{{authorId}}</div> </template> <script> export default { store, vuex: { getters: { authorId: state => state.authorId }, actions } } </script>mutation [types.CHANGE_AUTHORID] (state, value) { if (!/^\d*$/.test(value)) return
state.authorId = value
}目前暫時的解決方案:type="number" 不是通用解決方案(當需求不再是數字的情況)
vue 受限組件?
www說
2018-09-13 09:09:46