Vue Component Registration
https://vuejs.org/v2/guide/components-registration.html
Global Registration
So far, we’ve only created components using Vue.component
:
Vue.component('my-component-name', {
// ... options ...
})
These components are globally registered. That means they can be used in the template of any root Vue instance (new Vue
) created after registration. For example:
Vue.component('component-a', { /* ... */ })
Vue.component('component-b', { /* ... */ })
Vue.component('component-c', { /* ... */ })
new Vue({ el: '#app' })
<div id="app">
<component-a></component-a>
<component-b></component-b>
<component-c></component-c>
</div>
This even applies to all subcomponents, meaning all three of these components will also be available inside each other.
Local Registration
Global registration often isn’t ideal. For example, if you’re using a build system like Webpack, globally registering all components means that even if you stop using a component, it could still be included in your final build. This unnecessarily increases the amount of JavaScript your users have to download.
In these cases, you can define your components as plain JavaScript objects:
var ComponentA = { /* ... */ }
var ComponentB = { /* ... */ }
var ComponentC = { /* ... */ }
Then define the components you’d like to use in a components
option:
new Vue({
el: '#app',
components: {
'component-a': ComponentA,
'component-b': ComponentB
}
})
For each property in the components
object, the key will be the name of the custom element, while the value will contain the options object for the component.
Note that locally registered components are not also available in subcomponents. For example, if you wanted ComponentA
to be available in ComponentB
, you’d have to use:
var ComponentA = { /* ... */ }
var ComponentB = {
components: {
'component-a': ComponentA
},
// ...
}
Or if you’re using ES2015 modules, such as through Babel and Webpack, that might look more like:
import ComponentA from './ComponentA.vue'
export default {
components: {
ComponentA
},
// ...
}
Note that in ES2015+, placing a variable name like ComponentA
inside an object is shorthand for ComponentA: ComponentA
, meaning the name of the variable is both:
- the custom element name to use in the template, and
- the name of the variable containing the component options
Module Systems
If you’re not using a module system with import
/require
, you can probably skip this section for now. If you are, we have some special instructions and tips just for you.
Local Registration in a Module System
If you’re still here, then it’s likely you’re using a module system, such as with Babel and Webpack. In these cases, we recommend creating a components
directory, with each component in its own file.
Then you’ll need to import each component you’d like to use, before you locally register it. For example, in a hypothetical假设的 ComponentB.js
or ComponentB.vue
file:
import ComponentA from './ComponentA'
import ComponentC from './ComponentC'
export default {
components: {
ComponentA,
ComponentC
},
// ...
}
Now both ComponentA
and ComponentC
can be used inside ComponentB
‘s template.
Automatic Global Registration of Base Components
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 and they tend to be used very frequently across your components.
The result is that many components may include long lists of base components:
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:
<BaseInput
v-model="searchText"
@keydown.enter="search"
/>
<BaseButton @click="search">
<BaseIcon name="search"/>
</BaseButton>
Fortunately, if you’re using Webpack (or Vue CLI 3+, 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
):
import Vue from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'
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+$/, '')
)
)
// Register component globally
Vue.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
)
})
Remember that global registration must take place before the root Vue instance is created (with new Vue
). Here’s an example of this pattern in a real project context.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-12-11 What is the difference between UNION and UNION ALL?
2019-12-11 JavaScript多种继承方式
2019-12-11 js待学习
2018-12-11 sql server parameter validation of stored procedure
2018-12-11 SAML
2018-12-11 Get Started with ASP.NET Web API 2 (C#)
2017-12-11 win32 Service memory leak