{"id":16434203,"url":"https://github.com/lzxb/vue-watch-component","last_synced_at":"2026-05-12T12:03:11.194Z","repository":{"id":112517848,"uuid":"118326032","full_name":"lzxb/vue-watch-component","owner":"lzxb","description":"vue watch component","archived":false,"fork":false,"pushed_at":"2018-01-26T03:28:38.000Z","size":126,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-15T21:36:35.573Z","etag":null,"topics":["vue-router","vuejs","vuet","vuex"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lzxb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-21T10:48:11.000Z","updated_at":"2023-06-01T07:20:26.000Z","dependencies_parsed_at":"2023-05-15T20:30:47.620Z","dependency_job_id":null,"html_url":"https://github.com/lzxb/vue-watch-component","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lzxb%2Fvue-watch-component","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lzxb%2Fvue-watch-component/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lzxb%2Fvue-watch-component/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lzxb%2Fvue-watch-component/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lzxb","download_url":"https://codeload.github.com/lzxb/vue-watch-component/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240730834,"owners_count":19848423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["vue-router","vuejs","vuet","vuex"],"created_at":"2024-10-11T08:48:29.469Z","updated_at":"2026-05-12T12:03:06.159Z","avatar_url":"https://github.com/lzxb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## vue-watch-component\n[![Build Status](https://travis-ci.org/lzxb/vue-watch-component.svg)](https://travis-ci.org/lzxb/vue-watch-component)\n[![npm](https://img.shields.io/npm/v/vue-watch-component.svg)](https://www.npmjs.com/package/vue-watch-component) \n[![npm](https://img.shields.io/npm/dm/vue-watch-component.svg)](https://www.npmjs.com/package/vue-watch-component)\n[![npm](https://img.shields.io/npm/dt/vue-watch-component.svg)](https://www.npmjs.com/package/vue-watch-component)\n\n## 介绍\n只在组件激活时，观察数据变化\n\n## 使用\n``` bash\nnpm install --save vue-watch-component\n```\n\n```js\nimport Vue from 'vue'\nimport WatchComponent from 'vue-watch-component'\n\nVue.use(WatchComponent)\n\n// 1、创建一个Watch Component 实例\nconst watchOptins = {\n  value: 0, // 设置默认值，可选项\n  deep: false, // 是否检测对象内部的值发生变化，默认false，可选项\n  immediate: false, // 是否立即触发handler钩子回调，默认false，可选项\n  watch () { // 观察表达式，必选项\n    // 观察数据变化，基于vm.$watch实现\n    // this 会指向到注入的组件\n    return this.globalState.count\n  },\n  handler (newVal, oldVal) { // 处理value值变化的回调，必选项\n    // this 会指向到注入的组件\n    // 观察的数据发生变化，会调用此钩子\n  }\n}\n\nconst myWatch = new WatchComponent(watchOptins)\n\n// 2、全局状态，可以使用Vuet、vuex等第三方状态管理库\nconst globalState = {\n  count: 0\n}\n\n// 3、在组件中使用，只有在组件激活时，才会进行观察\nconst MyComponent = {\n  watchComponents: [\n    myWatch\n  ],\n  data () {\n    return {\n      globalState\n    }\n  },\n  // ... options\n}\n\n```\n\n`原理`，Watch Component会在组件的`created`、`activated`钩子触发观察，在组件的`deactivated`、`beforeDestroy`解除监听，并且将结果保存起来，等组件激活时重新进行观察\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flzxb%2Fvue-watch-component","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flzxb%2Fvue-watch-component","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flzxb%2Fvue-watch-component/lists"}