In the module test-party
, we demonstrate the basic process of frontend and backend development through a simple example:
- Implement a backend API and return
Hello World
- Create a frontend page, click the button, access the backend API, and display the returned
Hello World
Backend
1. Append API Route
src/suite-vendor/test-party/modules/test-party/backend/src/routes.js
- 1// test
- 22{ method: 'post', path: 'kitchen-sink/guide/echo', controller: 'testKitchensinkGuide' },
2. Append Controller Action
src/suite-vendor/test-party/modules/test-party/backend/src/controller/kitchen-sink/guide.js
- 1module.exports = app => {
- 2
- 3 class GuideController extends app.Controller {
- 4
- 5 async echo() {
- 6 const message = 'Hello World';
- 7 this.ctx.success(message);
- 8 }
- 9
- 10 }
- 11 return GuideController;
- 12};
src/suite-vendor/test-party/modules/test-party/backend/src/controllers.js
- 1const testKitchensinkGuide = require('./controller/kitchen-sink/guide.js');
- 2
- 3module.exports = app => {
- 4 const controllers = {
- 5 testKitchensinkGuide,
- 6 };
- 7 return controllers;
- 8};
Frontend
1. Append Page Route
src/suite-vendor/test-party/modules/test-party/front/src/routes.js
- 1function loadKitchenSink(name) {
- 2 return require(`./kitchen-sink/pages/${name}.vue`).default;
- 3}
- 4
- 5export default [
- 6 { path: 'kitchen-sink/guide', component: loadKitchenSink('guide') },
- 7];
2. Append Page Component
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 <f7-card>
- 5 <f7-card-content>
- 6 <div class="alert-info">{{message}}</div>
- 7 <p>
- 8 <eb-button :onPerform="onPerformClick">{{$text('Click')}}</eb-button>
- 9 </p>
- 10 </f7-card-content>
- 11 </f7-card>
- 12 </eb-page>
- 13</template>
- 14<script>
- 15export default {
- 16 data() {
- 17 return {
- 18 message: null,
- 19 };
- 20 },
- 21 methods: {
- 22 onPerformClick() {
- 23 return this.$api.post('kitchen-sink/guide/echo').then(data => {
- 24 this.message = data;
- 25 });
- 26 },
- 27 },
- 28};
- 29
- 30</script>
- 31<style scoped>
- 32</style>
CabloyJS frontend is based on VueJS + Framework7
, and according to the need of rapid development
, some components of Framework7
are customized
All page components must have a root node eb-page
. Here, we add a button. Response to button click event, access the backend API, and display the returned message
Effect
Open link http://localhost:9092/#!/test/party/kitchen-sink/guide, you can see the effect of frontend and backend communication
Comments: