{"id":19840425,"url":"https://github.com/heyui/hey-global","last_synced_at":"2025-02-28T19:18:58.681Z","repository":{"id":78553392,"uuid":"93020218","full_name":"heyui/hey-global","owner":"heyui","description":"控制全局变量","archived":false,"fork":false,"pushed_at":"2019-01-15T04:11:39.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-11T11:34:53.070Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/heyui.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}},"created_at":"2017-06-01T05:35:31.000Z","updated_at":"2019-03-14T13:47:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"f242f42a-3331-472c-a860-cd2497c311e5","html_url":"https://github.com/heyui/hey-global","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/heyui%2Fhey-global","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heyui%2Fhey-global/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heyui%2Fhey-global/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heyui%2Fhey-global/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/heyui","download_url":"https://codeload.github.com/heyui/hey-global/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241209891,"owners_count":19927749,"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":[],"created_at":"2024-11-12T12:26:50.535Z","updated_at":"2025-02-28T19:18:58.616Z","avatar_url":"https://github.com/heyui.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hey-global\njavascript全局变量控制\n\n\n## 安装\n\n``` shell\nnpm install --save hey-global\n```\n\n## Js引用\n\n直接使用script标签引用js文件，可以通过github下载，引用build/global.js文件\n\n使用的时候，直接引用G变量\n\n``` javascript\nG.set('GENDER', {\n  1: '先生',\n  2: '女士',\n})\n```\n\n## webpack配置建议\n一般使用hey-global的时候，我们建议定义成全局变量G，如果你使用的是webpack，可以做如下配置：\n``` javascript\nnew webpack.ProvidePlugin({\n  G: 'hey-global'\n});\n```\n\n## 方法\n\n### set(name, value) 存储变量\n\n``` javascript\nimport G from 'hey-global';\n// 如果是js直接引用的话，直接引用G变量\n// 或者直接配置webpack全局变量，则无需引用\n\nG.set('GENDER', {\n  1: '先生',\n  2: '女士',\n})\n\n// 存储用户信息\nG.set('Account', {});\n// 存储用户权限\nG.set('Admin', true);\n// 存储环境变量\nG.set('env', {\n  fileOs: 'http://www.download.com',\n  socketOs: 'ws://www.socket.com',\n});\n\n```\n\n### get(name) 获取变量\n\n``` javascript\n\nconst gender = G.get('GENDER')\n// {\n//   1: '先生',\n//   2: '女士',\n// }\n\n// 获取用户信息\nconst account = G.get('Account');\n// {}\n\n// 获取用户权限\nconst isAdmin = G.get('Admin');\n// true\n\n// 获取环境变量\nconst env = G.get('env');\n\n// {\n//   fileOs: 'http://www.download.com',\n//   socketOs: 'ws://www.socket.com',\n// }\n\n```\n\n### addlistener(name, func)  添加全局监听\n``` javascript\nlet todoTrigger = G.addlistener('NEW_TODO', (data)=\u003e{\n  console.log(`new todo: ${date.message}`)\n});\n\n```\n\n### trigger(name, param) 触发监听事件\n``` javascript\nG.trigger('NEW_TODO', {message: 'go home'});\n// new todo: go home\n```\n\n### removelistener(id) 删除监听事件\n当无需处理的时候，可以通过removelistener删除监听\n``` javascript\nG.removelistener(todoTrigger);\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheyui%2Fhey-global","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheyui%2Fhey-global","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheyui%2Fhey-global/lists"}