Page.Constants

12003

In this section, we’ll show how to use constants in your project and implement UI internationalization via pageHook. Constants are useful for storing text or data reused across multiple pages. By configuring constants in _constant_ui, you can conveniently reference them in pages. We’ll also demonstrate how to enable i18n by configuring language.

Using Constants

  • First, configure constants in _constant_ui. The table below defines a constant named constantDemo, which contains a copyright notice.
constantKey constantType pageId en zh
constantDemo object all { "copyright": "Copyright © 2022 jianghujs.org" } { "copyright": "版权声明 © 2022 jianghujs.org" }
  • Add /app/service/constantUi.js
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class ConstantUiService extends Service {
  4. async getConstantUiMap() {
  5. const { jianghuKnex } = this.app;
  6. const { pageId } = this.ctx.packagePage;
  7. const { language } = this.app.config;
  8. const constantUiList = await jianghuKnex(`_constant_ui`).whereIn('pageId', ['all', pageId]).select();
  9. const constantUiMap = Object.fromEntries(
  10. constantuIList.map(obj => {
  11. const { constantKey } = obj;
  12. try {
  13. return [constantKey, JSON.parse(obj[language] || '{}')];
  14. } catch (error) {
  15. this.app.logger.error('getConstantUiMap', ` constantKey:${constantKey} `, error);
  16. return [constantKey, {}];
  17. }
  18. })
  19. );
  20. return constantUiMap;
  21. }
  22. }
  23. module.exports = ConstantUiService;
  • Next, make sure the pageHook for the demoPage page is configured.
pageId pageType pageHook
demoPage showInMenu {"beforeHook":[{"field": "constantUiMap", "service": "constantUi", "serviceFunc": "getConstantUiMap"}]}
  • To use a constant on the page, reference the copyright text via <$ constantUiMap.constantDemo.copyright $>.
  1. <div>
  2. <h2>
  3. <$ constantUiMap.constantDemo.copyright $>
  4. </h3>
  5. </div>
  6. <script type="module">
  7. new Vue({
  8. el: '#app',
  9. template: '#app-template',
  10. vuetify: new Vuetify(),
  11. data: {
  12. },
  13. })
  14. </script>

Internationalizing Constants

To enable i18n, set language in /config/config.local.js. For example, to use English:

  1. language: 'en',

_constant_ui Table Schema

  1. CREATE TABLE `_constant_ui` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `constantKey` varchar(255) DEFAULT NULL,
  4. `constantType` varchar(255) DEFAULT NULL COMMENT '常量类型; object, array',
  5. `pageId` varchar(255) DEFAULT 'all' COMMENT '页面id',
  6. `desc` varchar(255) DEFAULT NULL COMMENT '描述',
  7. `en` text COMMENT '常量内容; object, array',
  8. `zh` text COMMENT '常量内容; object, array',
  9. `operation` varchar(255) DEFAULT 'insert' COMMENT '操作; insert, update, jhInsert, jhUpdate, jhDelete jhRestore',
  10. `operationByUserId` varchar(255) DEFAULT NULL COMMENT '操作者userId',
  11. `operationByUser` varchar(255) DEFAULT NULL COMMENT '操作者用户名',
  12. `operationAt` varchar(255) DEFAULT NULL COMMENT '操作时间; E.g: 2021-05-28T10:24:54+08:00 ',
  13. PRIMARY KEY (`id`) USING BTREE,
  14. UNIQUE KEY `pageId_constantKey_unique` (`constantKey`, `pageId`) USING BTREE
  15. ) ENGINE = InnoDB AUTO_INCREMENT = 15 DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMMENT = '常量表;'