registerModule中子模块名称允许字符范围及子模块键规则咨询
Great question — it’s totally fair to wonder about these unwritten (or hard-to-find) naming rules! Let’s start with the most common context for registerModule(): Vuex, Vue’s state management library.
First, a quick correction on parameters: in Vuex’s registerModule(), the module key (or path) is actually the first argument (either a string like 'checkout' or an array like ['admin', 'dashboard']), while the third argument is optional configuration (like preserveState). But regardless of the parameter position, the naming rules for the module key itself are consistent:
- The official docs don’t list strict, explicit character limits, but you’ll want to avoid spaces and special characters like
!@#$%^&*()entirely. These can cause issues with path parsing, debugging tools, or even unexpected behavior when accessing module state. - Recommended characters to stick with:
- Uppercase and lowercase letters (A-Z, a-z)
- Numbers (0-9) — though it’s best not to start a key with a number, since that conflicts with JavaScript’s identifier rules (module keys become properties on the state tree, after all)
- Underscores (
_) and hyphens (-) — hyphens work but require bracket notation to access (e.g.,$store.state['user-settings']), so underscores are more convenient for snake_case naming, or you can use camelCase likeuserSettings
In practice, following JavaScript’s identifier naming conventions (with the exception of hyphens if you don’t mind bracket notation) is a safe bet. This ensures your module keys play nicely with the framework’s internal logic and are easy for other developers to read and work with.
Examples of good keys: user_profile, productCatalog, order_789
Examples of keys to avoid: user settings (spaces), admin@panel (special symbols), 123_cart (starts with number)
If you’re using a different framework or custom registerModule() function, the rules might shift slightly, but the core principle stays the same: keep names clean, free of ambiguous characters, and compatible with how the framework parses or references module keys.
内容的提问来源于stack exchange,提问作者Sybille Peters

