1. <template>
  2. <div>
  3. <input type="text" v-model="word" placeholder="请输入" />
  4. {{msg}}
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: "Home",
  10. data() {
  11. return {
  12. word: "",
  13. msg: ""
  14. };
  15. },
  16. watch: {
  17. word(newVal, oldVal) {
  18. setTimeout(() => {
  19. if (newVal.length > 6) {
  20. return (this.msg = "字符不能大于6位");
  21. } else if (newVal.length < 3) {
  22. return (this.msg = "字符不能小于3位");
  23. }
  24. }, 1000);
  25. }
  26. }
  27. };
  28. </script>

深度监听

  1. watch:{
  2. arr:{
  3. handler(newVal){
  4. },
  5. deep:true
  6. }
  7. }