Skip to content

Commit 043bb46

Browse files
committed
2 parents 5dec988 + e4f1540 commit 043bb46

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/components.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
import Vue from 'vue'
2-
import HelloWorld from './components/HelloWorld.vue'
3-
// Import all components here
42

5-
Vue.component('HelloWorld', HelloWorld)
6-
// Attach all components to Vue here
3+
// Automatically register all Vue components located within the /src/components folder.
4+
5+
const requireComponent = require.context(
6+
// The relative path of the components folder
7+
'./components',
8+
// Whether or not to look in subfolders
9+
true,
10+
// The regular expression used to match base component filenames
11+
/[A-Z]\w+\.(vue|js)$/
12+
)
13+
14+
requireComponent.keys().forEach(fileName => {
15+
// Get component config
16+
const componentConfig = requireComponent(fileName)
17+
18+
const cleanFileName = fileName.replace(/^\.\/(.*)\.\w+$/, '$1')
19+
20+
// Register component globally
21+
Vue.component(
22+
cleanFileName,
23+
// Look for the component options on `.default`, which will
24+
// exist if the component was exported with `export default`,
25+
// otherwise fall back to module's root.
26+
componentConfig.default || componentConfig
27+
)
28+
})

0 commit comments

Comments
 (0)