### Install @seed-fe/global Source: https://github.com/xianghongai/seed-fe-global/blob/main/README.md Instructions for installing the @seed-fe/global package using npm, yarn, or pnpm. ```bash # 使用 npm npm install @seed-fe/global # 使用 yarn yarn add @seed-fe/global # 使用 pnpm (推荐) pnpm add @seed-fe/global ``` -------------------------------- ### Set and Get Global Variables with @seed-fe/global Source: https://github.com/xianghongai/seed-fe-global/blob/main/README.md Demonstrates how to set, get, check for the existence of, and remove global variables using the @seed-fe/global library. It includes examples for individual variables and entire scopes. ```typescript import global from '@seed-fe/global'; // 设置全局变量 global.set('myApp', 'config', { apiUrl: 'https://api.example.com' }); // 获取全局变量 const config = global.get('myApp', 'config'); // 或直接访问 myApp.config 对象 // 检查全局变量是否存在 if (global.has('myApp', 'config')) { // 存在 } // 删除全局变量 global.remove('myApp', 'config'); // 删除整个命名空间 global.remove('myApp'); // 一次性设置多个变量 global.setMany('myApp', { version: '1.0.0', debug: true, config: { apiUrl: 'https://api.example.com' } }); // 获取命名空间下的所有变量 const allValues = global.getAll('myApp'); // allValues = { version: '1.0.0', debug: true, config: { apiUrl: 'https://api.example.com' } } ``` -------------------------------- ### API: get(scope, key) Source: https://github.com/xianghongai/seed-fe-global/blob/main/README.md The `get` method retrieves a specific global variable. It requires a scope (string) and a key (string). If the variable exists, its value is returned; otherwise, `undefined` is returned. ```typescript const config = global.get('myApp', 'config'); ``` -------------------------------- ### API: getAll(scope) Source: https://github.com/xianghongai/seed-fe-global/blob/main/README.md The `getAll` method retrieves all variables within a given scope. It takes a scope (string) and returns an object containing all key-value pairs for that scope, or `undefined` if the scope does not exist. ```typescript const allValues = global.getAll('myApp'); ``` -------------------------------- ### API: set(scope, key, value) Source: https://github.com/xianghongai/seed-fe-global/blob/main/README.md The `set` method in @seed-fe/global is used to set a global variable. It takes a scope (string), a key (string), and a value (any type) and returns the set value. ```typescript global.set('myApp', 'config', { apiUrl: 'https://api.example.com' }); ``` -------------------------------- ### API: has(scope, key?) Source: https://github.com/xianghongai/seed-fe-global/blob/main/README.md The `has` method checks for the existence of a global variable or an entire scope. It accepts a scope (string) and an optional key (string). It returns `true` if the variable or scope exists, and `false` otherwise. ```typescript // 检查特定变量是否存在 if (global.has('myApp', 'config')) { // 存在 } // 检查命名空间是否存在 if (global.has('myApp')) { // 存在 } ``` -------------------------------- ### API: setMany(scope, values) Source: https://github.com/xianghongai/seed-fe-global/blob/main/README.md The `setMany` method allows setting multiple global variables at once within a specified scope. It accepts a scope (string) and an object containing key-value pairs, returning the object that was set. ```typescript global.setMany('myApp', { version: '1.0.0', debug: true, config: { apiUrl: 'https://api.example.com' } }); ``` -------------------------------- ### Global Variable Protection Mechanism Source: https://github.com/xianghongai/seed-fe-global/blob/main/README.md Illustrates the protection mechanism of @seed-fe/global, which uses proxies to prevent direct modification of global variables. It shows how attempts to directly alter variables are blocked, and the correct way to update them using the library's API. ```typescript // 设置全局变量 global.set('myApp', 'version', '1.0.0'); // 尝试直接修改(会被阻止并显示警告) window.myApp.version = '2.0.0'; // 不会生效 // 正确的修改方式 global.set('myApp', 'version', '2.0.0'); ``` -------------------------------- ### API: remove(scope, key?) Source: https://github.com/xianghongai/seed-fe-global/blob/main/README.md The `remove` method deletes a global variable or an entire scope. It takes a scope (string) and an optional key (string). It returns `true` if the deletion was successful, and `false` otherwise. ```typescript // 删除特定变量 global.remove('myApp', 'config'); // 删除整个命名空间 global.remove('myApp'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.