-
Notifications
You must be signed in to change notification settings - Fork 4.8k
docs: add automatic global registration to cookbook #1041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/cookbook/automatic-global-registration-of-base-components.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Automatic Global Registration of Base Components | ||
|
||
## Base Example | ||
|
||
Many of your components will be relatively generic, possibly only wrapping an element like an input or a button. We sometimes refer to these as [base components](../style-guide/#Base-component-names-strongly-recommended) and they tend to be used very frequently across your components. | ||
|
||
The result is that many components may include long lists of base components: | ||
|
||
```js | ||
import BaseButton from './BaseButton.vue' | ||
import BaseIcon from './BaseIcon.vue' | ||
import BaseInput from './BaseInput.vue' | ||
export default { | ||
components: { | ||
BaseButton, | ||
BaseIcon, | ||
BaseInput | ||
} | ||
} | ||
``` | ||
|
||
Just to support relatively little markup in a template: | ||
|
||
```html | ||
<BaseInput v-model="searchText" v-on:keydown.enter="search" /> | ||
<BaseButton v-on:click="search"> | ||
<BaseIcon name="search" /> | ||
</BaseButton> | ||
vilaboim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
Fortunately, if you're using Webpack (or [Vue CLI](https://github.com/vuejs/vue-cli), which uses Webpack internally), you can use `require.context` to globally register only these very common base components. Here's an example of the code you might use to globally import base components in your app's entry file (e.g. `src/main.js`): | ||
vilaboim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```js | ||
import { createApp } from 'vue' | ||
import upperFirst from 'lodash/upperFirst' | ||
import camelCase from 'lodash/camelCase' | ||
import App from './App.vue' | ||
|
||
const app = createApp(App) | ||
|
||
const requireComponent = require.context( | ||
// The relative path of the components folder | ||
'./components', | ||
// Whether or not to look in subfolders | ||
false, | ||
// The regular expression used to match base component filenames | ||
/Base[A-Z]\w+\.(vue|js)$/ | ||
) | ||
|
||
requireComponent.keys().forEach(fileName => { | ||
// Get component config | ||
const componentConfig = requireComponent(fileName) | ||
|
||
// Get PascalCase name of component | ||
const componentName = upperFirst( | ||
camelCase( | ||
// Gets the file name regardless of folder depth | ||
fileName | ||
.split('/') | ||
.pop() | ||
.replace(/\.\w+$/, '') | ||
) | ||
) | ||
|
||
app.component( | ||
componentName, | ||
// Look for the component options on `.default`, which will | ||
// exist if the component was exported with `export default`, | ||
// otherwise fall back to module's root. | ||
componentConfig.default || componentConfig | ||
) | ||
}) | ||
|
||
app.mount('#app') | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.