CabloyJS’s business module
includes not only backend logic, but also frontend pages. In order to improve the development efficiency and flexibility of business module
, it not only provides the features of configuration
and i18n
in the backend, but also provides the features in the frontend, and also supports project level
overriding
Here, we implement a virtual requirement:
- Put the parameter
message
in the frontend configuration - The frontend defines another parameter
markCount
, which indicates the number of exclamation marks - The frontend passes these two parameters to the backend, which combines them and returns the result
- The frontend displays the returned result
Configuration
1. Definition of Config
src/suite-vendor/test-party/modules/test-party/front/src/config/config.js
- 1export default {
- 2 message: 'Hello World',
- 3 markCount: 2,
- 4};
2. Usage of Config
src/suite-vendor/test-party/modules/test-party/front/src/kitchen-sink/pages/guide.vue
- 1onPerformClick4() {
- 2 const params = {
- 3 message: this.$config.message,
- 4 markCount: this.$config.markCount,
- 5 };
- 6 return this.$api.post('kitchen-sink/guide/echo4', params).then(data => {
- 7 this.message4 = data;
- 8 });
- 9},
3. Backend Logic
src/suite-vendor/test-party/modules/test-party/backend/src/controller/kitchen-sink/guide.js
- 1async echo4() {
- 2 const { message, markCount } = this.ctx.request.body;
- 3 const res = `${message}${new Array(markCount + 1).join('!')}`;
- 4 this.ctx.success(res);
- 5}
4. Override Config
Use project level
config to override module level
config, thereby changing message
from Hello World
to Hello World!
, and changing markCount
from 2
to 3
src/front/config/config.{scene}.js
- 1export default{
- 2 modules: {
- 3 'test-party': {
- 4 message: 'Hello World!',
- 5 markCount: 3,
- 6 },
- 7 },
- 8};
i18n
1. Definition of Language Resources
The default language for CabloyJS is en-us
. If support Chinese
, you need to add zh-cn
resource file
src/suite-vendor/test-party/modules/test-party/front/src/config/locale/zh-cn.js
- 1export default {
- 2 'Hello World': '世界,您好',
- 3};
2. Usage of Language Resources
CabloyJS uses the method $text
to dynamically get the specified language resources according to the locale
configuration of the client
src/suite-vendor/test-party/modules/test-party/front/src/kitchen-sink/pages/guide.vue
- 1onPerformClick5() {
- 2 const params = {
- 3 message: this.$text('Hello World'),
- 4 markCount: this.$config.markCount,
- 5 };
- 6 return this.$api.post('kitchen-sink/guide/echo4', params).then(data => {
- 7 this.message4 = data;
- 8 });
- 9},
3. Override Language Resources
Use project level
language resources to override module level
language resources, thereby changing 世界,您好
to 您好,世界
src/front/config/locale/zh-cn.js
- 1export default {
- 2 'Hello World': '您好,世界',
- 3};
4. More Examples
Similarly, we can i18n the title
and button
of the test page
src/suite-vendor/test-party/modules/test-party/front/src/config/locale/zh-cn.js
- 1export default {
- 2 Guide: '指南',
- 3 Click: '点击',
- 4};
src/suite-vendor/test-party/modules/test-party/front/src/kitchen-sink/pages/guide.vue
- 1<template>
- 2 <eb-page>
- 3 <eb-navbar :title="$text('Guide')" eb-back-link="Back"></eb-navbar>
- 4 ...
- 5 <eb-button :onPerform="onPerformClick">{{$text('Click')}}</eb-button>
- 6 ...
- 7 </eb-page>
- 8</template>
Comments: