当前位置:   article > 正文

vue watch监听的多种使用

vue watch监听的多种使用

简述:vue 的watch的监听使用的几种写法。常用第4中写法。 

一、$route监听路由跳转

前提:当需要前端监听路由跳转的时候,一般写在App.vue入口

  1. //App.vue
  2. //vue2、uniapp写法
  3. watch: {
  4. $route(to, from) {
  5. if (hasPermission(to.path)) {
  6. this.$store.commit("setIisShipGuid", false);
  7. } else {
  8. this.$store.commit("setIisShipGuid", true);
  9. }
  10. },
  11. },

二、监听store中的某些变量

  1. //vue2、uniapp写法
  2. watch: {
  3. "$store.state.isShipGuid": {
  4. handler(newVal, oldVal) {
  5. if (newVal !== oldVal) {
  6. this.isShipGuid = newVal;
  7. }
  8. },
  9. immediate: true,
  10. deep: true,
  11. },
  12. },
  1. //vue3 setup写法
  2. watch(
  3. () => store.selectShip.mmsi,
  4. (oldNum, newNum) => {
  5. if (oldNum != newNum) {
  6. try {
  7. cabin.getEngineRoomMenu(store.selectShip.mmsi);
  8. } catch (error) {
  9. cabin.getEngineRoomMenu(store.selectShip.mmsi);
  10. }
  11. }
  12. },
  13. { immediate: true, deep: true }
  14. );

三、自定义组件内部监听传参

  1. //封装的一个图表组件。监听传参
  2. //vue2、uniapp
  3. export default {
  4. props: {
  5. height: {
  6. type: String,
  7. default: "100%",
  8. },
  9. width: {
  10. type: String,
  11. default: "100%",
  12. },
  13. echartsId: {
  14. type: String,
  15. required: true,
  16. },
  17. series: {
  18. type: Array,
  19. default: [],
  20. },
  21. title: {
  22. type: Object,
  23. default: null,
  24. },
  25. },
  26. //监听传参
  27. watch: {
  28. myOption: function (newVal) {
  29. if (newVal) {
  30. console.log(
  31. "=================================================================="
  32. );
  33. this.myCharts.clear();
  34. this.init();
  35. }
  36. },
  37. series: function (newVal) {
  38. if (newVal) {
  39. console.log(
  40. "=================================================================="
  41. );
  42. this.myCharts.clear();
  43. this.init();
  44. }
  45. },
  46. },
  47. }
  1. //vue3写法
  2. //监听多个参数的数组的写法
  3. watch(
  4. [() => props.modelValue, () => data.defaultFileList],
  5. ([newValue1, newValue2]) => {
  6. console.log("newValue1", newValue1, "newValue2", newValue2);
  7. },
  8. { deep: true }
  9. );

四、监听单个页面组件内部的data()

(一)、监听某一个对象

  1. data() {
  2. return {
  3. isActive: 0,
  4. }}
  5. //vue2、uniapp写法
  6. //vue2在单个页面组件内部,只能有一个watch
  7. //vue3在单个页面组件内部,可以有多个watch
  8. watch: {
  9. isActive(newval) {
  10. this.currentIndex = newval;
  11. },
  12. "$store.state.mmsi": {
  13. handler(newVal, oldVal) {
  14. if (newVal !== oldVal) {
  15. this.mmsi = newVal;
  16. this.getEngineRoomMenu(this.mmsi);
  17. }
  18. },
  19. immediate: true,
  20. deep: true,
  21. },
  22. },

(二)、监听一个对象的某一个属性

  1. //vue、uniapp写法
  2. watch: {
  3. 'search.name': {
  4. handler() {
  5. // todo
  6. },
  7. }
  8. }
  9. 或者:
  10. computed: {
  11. getName: function() {
  12. return this.search.name
  13. }
  14. }
  15. watch: {
  16. getName: {
  17. handler: function() {
  18. //todo
  19. },
  20. }
  21. }

 

  1. const data = reactive({
  2. ruleForm: {
  3. param: {},
  4. },
  5. )
  6. //vue3写法
  7. watch(
  8. () => data.ruleForm.param.publishType,
  9. (newName, oldName) => {
  10. if (newName === 1) {
  11. data.useridsList_display = true;
  12. } else if (newName === 0) {
  13. data.ruleForm.param.useridsList = [];
  14. data.useridsList_display = false;
  15. }
  16. },
  17. {
  18. immediate: true,
  19. deep: true,
  20. }
  21. );

其它, 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/386383?site
推荐阅读
相关标签
  

闽ICP备14008679号