AppGallery Connect Remote Configuration allows you to manage parameters online. With the service, you can change the behavior and appearance of your app online without requiring users to update the app.
Remote Configuration provides cloud-based services, the console, and the client SDK. By integrating the client SDK, your app can periodically fetch parameter values delivered on the console to modify the app's behavior and appearance.
In this codelab, you will create an app that integrates Remote Configuration of AppGallery Connect and fetch parameter values. Your app will be able to:
To integrate Remote Configuration of AppGallery Connect, you must complete the following preparations:
Run the following command to integrate the Remote Configuration JavaScript SDK into your project:
npm install --save @agconnect/remoteconfig@1.2.1
In this codelab, the third-party open-source layout library based on Element UI is used. You need to import the tool first.
"element-ui": "^2.13.2"
Enter the following command:
npm install
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
<template>
<div class="remoteconfigContainer">
<el-form :model="dataForm_sdk" :rules="rules" status-icon label-position="left" label-width="80px"
class="demo-ruleForm remoteconfig-page">
<h3 class="title">RemoteConfigCodelab</h3>
<el-form-item label="FetchConfig" label-width="200px" class="fetchConfigTitle">
</el-form-item>
<el-form-item>
<el-button type="primary" @click="fetch" class="fetchButton">Fetch</el-button>
<el-button type="primary" @click="applyConfig" class="applyConfigButton">Apply</el-button>
</el-form-item>
<el-form-item label="DefaultConfig" label-width="200px" class="setDefaultVauleTitle">
</el-form-item>
<el-form-item>
<el-input type="textarea" :disabled="false" v-model="dataForm_sdk.defaultConfig"
placeholder="Please enter JSON format string"
class="inputValueText"></el-input>
</el-form-item>
<el-button type="primary" @click="applyDefault" class="applyDefaultButton">applyDefault</el-button>
<el-form-item label="GetVaule" class="getVauleTitle">
</el-form-item>
<el-form-item>
<el-input type="textarea" :disabled="false" autosize="autosize" v-model="dataForm_sdk.valueText"
class="getValueText"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="getValue" class="getValueButton">Show Value</el-button>
<el-button @click="clearAll" class="clearValueButton">ClearAll</el-button>
</el-form-item>
<el-form-item>
</el-form-item>
</el-form>
</div>
</template>
The code in style is as follows:
<style scoped>
.remoteconfig-page {
-webkit-border-radius: 5px;
border-radius: 5px;
margin: auto;
width: 800px;
padding: 35px 35px 15px;
background: #fff;
border: 1px solid #eaeaea;
box-shadow: 0 0 25px #cac6c6;
}
.getVauleTitle,
.fetchConfigTitle,
.setDefaultVauleTitle {
border-bottom: 1px solid #eaeaea;
}
.applyConfigButton,
.fetchButton {
width: 120px;
}
.getValueButton {
margin-left: 30px;
width: 120px;
}
.applyDefaultButton {
margin-left: 530px;
width: 120px;
}
.getValueText,
.inputValueText {
width: 600px;
}
</style>
import agconnect from "@agconnect/api";
import "@agconnect/remoteconfig";
import "@agconnect/instance";
data () {
return {
dataForm_sdk: {
valueText: '',
defaultConfig: ''
},
rules: {},
};
},
async created() {
var agConnectConfig =
{
// App configurations.
};
agconnect.instance().configInstance(agConnectConfig);
},
applyDefault() {
if (this.dataForm_sdk.defaultConfig != '') {
if (typeof this.dataForm_sdk.defaultConfig == 'string') {
try {
var defaultConfigJson = JSON.parse(this.dataForm_sdk.defaultConfig);
if (typeof defaultConfigJson == 'object' && defaultConfigJson) {
let defaultConfigMap = new Map();
for (var k in defaultConfigJson) {
defaultConfigMap.set(k, JSON.stringify(defaultConfigJson[k]));
}
agconnect.remoteConfig().applyDefault(defaultConfigMap);
alert('applyDefault success');
} else {
alert('please input JSON string!');
this.dataForm_sdk.defaultConfig = '';
}
} catch (e) {
alert('please input JSON string!');
this.dataForm_sdk.defaultConfig = '';
}
}
} else {
alert('please input JSON string!');
this.dataForm_sdk.defaultConfig = '';
}
},
async fetch() {
agconnect.remoteConfig().minFetchIntervalMillis = 1000
await agconnect.remoteConfig().fetch().then((res) => {
alert('fetch successfully!');
}).catch((err) => {
alert(JSON.stringify(err));
});
},
Then call the apply method to update the parameter value from Remote Configuration.
applyConfig() {
return agconnect.remoteConfig().apply().then((res) => {
if (res) {
alert('apply successfully!');
} else {
alert('apply failed!');
}
}
).catch(error => {
alert(JSON.stringify(error));
});
},
getValue () {
let obj = Object.create(null);
let resultMap = agconnect.remoteConfig().getMergedAll();
for (let [k, v] of resultMap) {
obj[k] = v.value;
}
this.dataForm_sdk.valueText = JSON.stringify(obj);
},
clearAll() {
agconnect.remoteConfig().clearAll().then(()=>{
alert('clearAll OK');
});
}
npm start
After the compilation is complete, click the generated link.
Well done. You have successfully built your first app that integrates Remote Configuration and learned how to:
For more information, please refer to the following links: