|
| 1 | +--- |
| 2 | +title: About module |
| 3 | +description: 'Create About module' |
| 4 | +position: 14 |
| 5 | +category: Cookbook |
| 6 | +--- |
| 7 | + |
| 8 | +The second module will be called `About`. |
| 9 | + |
| 10 | +> All files to be added are located in the `src` directory. |
| 11 | +
|
| 12 | +<alert type="info" align="center"> |
| 13 | + <img src="demo/image/demo-about-module.png" alt="About module"/> |
| 14 | +</alert> |
| 15 | + |
| 16 | +## Initiate file |
| 17 | + |
| 18 | +First you need to create an [initialization file](/module-config#indexjs). |
| 19 | + |
| 20 | +```javascript [@About/index.js] |
| 21 | + export default async function () {} |
| 22 | +``` |
| 23 | +> The function is empty because we do not run any additional mechanisms before this module. |
| 24 | +
|
| 25 | +## Configuration |
| 26 | + |
| 27 | +We create a `config` directory and in it we create an `index.js` file that contains all the configurations. |
| 28 | + |
| 29 | + |
| 30 | +> More information about [configuration](/module-config#main-configuration). |
| 31 | +
|
| 32 | +### `name` |
| 33 | +Set the name of the module. The name should match the directory structure. |
| 34 | +```javascript |
| 35 | + name: '@example2/about', |
| 36 | +``` |
| 37 | + |
| 38 | +### `order` |
| 39 | +Specify the order in which the module is loaded. The smaller the value, the earlier the module is loaded. |
| 40 | +```javascript |
| 41 | + order: 20, |
| 42 | +``` |
| 43 | + |
| 44 | +### `aliases` |
| 45 | +Specifies the alias to refer to in other modules. |
| 46 | +It is needed for easy communication between modules |
| 47 | + |
| 48 | +```javascript |
| 49 | + aliases: { |
| 50 | + '@About': '/', |
| 51 | + }, |
| 52 | +``` |
| 53 | + |
| 54 | +#### Example: |
| 55 | + |
| 56 | +```javascript [@About/config/index.js] |
| 57 | + export default { |
| 58 | + name: '@example2/about', |
| 59 | + order: 20, |
| 60 | + aliases: { |
| 61 | + '@About': '/', |
| 62 | + }, |
| 63 | + } |
| 64 | +``` |
| 65 | + |
| 66 | +## Register module |
| 67 | + |
| 68 | +After creating the module, we now need to add it to the **VueMS** configuration. |
| 69 | +Registration is done in the **VueMS** configuration in the `nuxt.config.js` file. |
| 70 | + |
| 71 | +Add the module name to the configuration. |
| 72 | + |
| 73 | +```javascript [nuxt.config.js] |
| 74 | + export default { |
| 75 | + vuems: { |
| 76 | + modules: { |
| 77 | + local: ['@example/core', '@example2/about'], |
| 78 | + }, |
| 79 | + required: ['@example/core'], |
| 80 | + logLoadedModules: true, |
| 81 | + }, |
| 82 | + } |
| 83 | +``` |
| 84 | + |
| 85 | +## Component |
| 86 | + |
| 87 | +Create the first components in `components` directory. |
| 88 | + |
| 89 | +### `About.vue` |
| 90 | +Create a `About.vue` file in `component` directory. |
| 91 | + |
| 92 | +```vue [@About/components/About.vue] |
| 93 | +<template> |
| 94 | + <span class="subtitle">It's me</span> |
| 95 | +</template> |
| 96 | +
|
| 97 | +<script> |
| 98 | +export default { |
| 99 | + name: 'About', |
| 100 | +} |
| 101 | +</script> |
| 102 | +``` |
| 103 | + |
| 104 | +## Page |
| 105 | + |
| 106 | +The next thing is to add a new page. |
| 107 | +We create a `pages` directory where we add the page. |
| 108 | + |
| 109 | +In the new page component, we add our previously created `Wrapper` component. |
| 110 | + |
| 111 | +```vue [@About/pages/about/index.vue] |
| 112 | +<template> |
| 113 | + <Wrapper> |
| 114 | + <h1 class="title">About</h1> |
| 115 | + <About /> |
| 116 | + </Wrapper> |
| 117 | +</template> |
| 118 | +
|
| 119 | +<script> |
| 120 | +import Wrapper from '@Core/components/Wrapper' |
| 121 | +import About from '@About/components/About' |
| 122 | +
|
| 123 | +export default { |
| 124 | + name: 'AboutPage', |
| 125 | + components: { |
| 126 | + Wrapper, |
| 127 | + About, |
| 128 | + }, |
| 129 | +} |
| 130 | +</script> |
| 131 | +
|
| 132 | +<style> |
| 133 | +.title { |
| 134 | + font-family: 'Quicksand', 'Source Sans Pro', -apple-system, BlinkMacSystemFont, |
| 135 | + 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; |
| 136 | + display: block; |
| 137 | + font-weight: 300; |
| 138 | + font-size: 60px; |
| 139 | + color: #35495e; |
| 140 | + letter-spacing: 1px; |
| 141 | +} |
| 142 | +</style> |
| 143 | +
|
| 144 | +``` |
| 145 | + |
| 146 | +## Router |
| 147 | + |
| 148 | +For the page we created in the previous step to work, we need to add routing to it. |
| 149 | + |
| 150 | +To do this we add the `routes.js` file in the `config` directory. |
| 151 | + |
| 152 | +> More information [here](/module-config#routing) |
| 153 | +
|
| 154 | +```javascript [@About/config/routes.js] |
| 155 | +import { Pages } from './imports' |
| 156 | + |
| 157 | +export const ROUTE_NAME = { |
| 158 | + ABOUT: 'about', |
| 159 | +} |
| 160 | + |
| 161 | +export default [ |
| 162 | + { |
| 163 | + name: ROUTE_NAME.ABOUT, |
| 164 | + path: '/about', |
| 165 | + component: Pages.About, |
| 166 | + }, |
| 167 | +] |
| 168 | +``` |
| 169 | + |
| 170 | +To keep things organized, we add a helper file for importing components. |
| 171 | +In the `config` directory, add the `imports.js` file. |
| 172 | + |
| 173 | +```javascript [@About/config/imports.js] |
| 174 | +export const Pages = { |
| 175 | + About: () => import('@About/pages/about').then((m) => m.default || m), |
| 176 | +} |
| 177 | +``` |
0 commit comments