Skip to content
This repository was archived by the owner on Mar 8, 2025. It is now read-only.

Commit ce6d0e7

Browse files
committed
🔨 Auto register base components
1 parent 113ac0b commit ce6d0e7

File tree

12 files changed

+85
-16
lines changed

12 files changed

+85
-16
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ pnpm-debug.log*
2121
*.njsproj
2222
*.sln
2323
*.sw?
24+
25+
temp.js

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ Core folder:
6767

6868
>Code will be put in the core folder if create a new feature, a new module doesn't make change to that code
6969
70+
## Conventions
7071

71-
### Customize configuration
72+
> Keep things clear, annotatable, highlightable by editor, be friendly with devTools
7273
73-
See [Configuration Reference](https://cli.vuejs.org/config/).
74+
- Checkout [Nuxt.js](https://nuxtjs.org) for more naming convention
75+
- Use **PascalCase** for component name for better editor annotation and highlighting support
76+
- Use **camelCase** for variables and functions
77+
- Use `export const` instead of `export default` for auto import support
78+
- Use `My` for custom component prefix, for example: `MyCustomComponent`
79+
- Use `my-` for events prefix for better filtering in devTools, for example: `my-blog-item-click`

src/core/components/base/auth/Auth.vue

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default as Auth } from './Auth.vue'
1+
export { default } from './src/index.vue'
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<script>
2+
import { store } from '@/store'
3+
import { defineComponent } from '@vue/composition-api'
4+
import _intersectionWith from 'lodash/intersectionWith'
5+
import _isEqual from 'lodash/isEqual'
6+
7+
export default defineComponent({
8+
name: 'Auth',
9+
functional: true,
10+
props: {
11+
allow: {
12+
type: Array,
13+
default() {
14+
return []
15+
},
16+
},
17+
},
18+
render(h, { props, slots }) {
19+
// Example:
20+
// store.state.auth.data.user.permissions: [1, 2, 3, 4]
21+
// this.allow: [1, 3, 5, 7]
22+
// _intersectionWith: [1, 3] => is allowed (has allowed permissions)
23+
24+
const commonPermissions = _intersectionWith(
25+
props.allow,
26+
store.state.auth?.data?.user?.permissions, // Modify this base on your backend
27+
_isEqual
28+
)
29+
30+
return commonPermissions?.length > 0 ? slots()?.default : null
31+
},
32+
})
33+
</script>

src/core/components/base/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Vue from 'vue'
2+
import _kebabCase from 'lodash/kebabCase'
3+
4+
// We don't scan .vue file directly because:
5+
// A component can have sub-components that are not exported
6+
// An index.js file can decide what to export and be named
7+
8+
const registerGlobalBaseComponents = () => {
9+
// Every index.js file
10+
const requireModule = require.context('./', true, /index\.js$/)
11+
12+
requireModule.keys().forEach((fileName) => {
13+
if (fileName !== './index.js') {
14+
const module = requireModule(fileName).default
15+
16+
// Remove "./" and "index.js"
17+
const moduleName = _kebabCase(fileName.replace(/(\.\/|index.js)/g, ''))
18+
19+
Vue.component(moduleName, module)
20+
}
21+
})
22+
}
23+
24+
registerGlobalBaseComponents()

src/core/utils/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ export const utils = {
2121
case 'directives':
2222
Vue.directive(moduleName, module)
2323
break
24-
case 'global-components':
25-
Vue.component(moduleName, module)
26-
break
2724
case 'functions':
2825
Vue.prototype[`$${moduleName}`] = module
2926
break

src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import '@/core/styles/scss/main.scss'
2323

2424
import '@/styles/custom.scss'
2525

26+
import '@/core/components/base'
27+
2628
Vue.mixin(globalMixin)
2729

2830
Vue.use(clientApi)

src/pages/dashboard/index.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<template>
22
<div class="dashboard-page">
3-
<h1>This is an dashboard page</h1>
3+
<auth :allow="['SEE_STUFF']">
4+
<h1>This is an dashboard page</h1>
5+
</auth>
46
</div>
57
</template>
68
<script>

src/store/auth/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getters } from './getters'
33
import { mutations } from './mutations'
44
import { actions } from './actions'
55
export default {
6+
namespaced: true,
67
state,
78
getters,
89
mutations,

0 commit comments

Comments
 (0)