I don’t know how everyone writes mini programs. I was writing WeChat mini programs a few months ago. I created a structure, or framework, by myself. The native architecture of WeChat is like this
├── app.js ├── app.json ├── app.wxss ├── pages │ │── index │ │ ├── index.wxml │ │ ├── index.js │ │ ├── index.json │ │ └── index.wxss │ └── logs │ ├── logs.wxml │ └── logs.js └── utils
Why not use WeChat’s native writing method?
Mini programs are similar to mobile apps, they all have a tabBar, right? WeChat’s official tabBar is configured globally in app.json. There is no problem in general development, but when it comes to a complex tabBar, it cannot be implemented using the official native one, and it cannot be loaded dynamically, such as the photo and video function in the middle of a video app.
This means that we cannot use the tabBar provided by the official, we need to write our own
At first, my approach was to keep the original structure and just write the tabBar myself, so the page code was written in a file. The first screen was displayed by default, and the others were hidden. When switching pages, the corresponding page was displayed, the others were hidden, and the data was dynamically rendered. But there is a problem. If you have a small project, there will be no big problem. But if it is a large project, the amount of code is very large, and it is all written in one file. It is difficult to maintain later, so this method was eventually passed.
Later, the method of switching pages was changed to jump (wx.switchTab, etc.), and the codes of different pages were placed in different files. However, there was another problem. The switching would also flash. Every time the page was switched, it was like reopening a web page. The tabBar was re-rendered, so it would flash. pass
As a result, there is now a new architectural approach: Define all the files under /pages/index/ as entry files, js entry, css entry, and view entry. Files for different pages should be placed in different locations. For easier management, a new template folder is created to store the code between different pages. The structure is the same as the official one but the page structure is the same. app.js in the root directory is used to store global functions, and other page calls only require getApp() js entry file
const app = getApp(); var index_js = require("../../template/index/index.js"); var types_js = require("../../template/types/types.js"); var Global_Data = []; Page({ data: { active: 0, show: { index: true, types: false, course: false, user: false } },
onLoad(options) {
this.setData({
Global\_Data: index\_js.getData()
})
},
// Bottom nav switch
tabbar\_onChange(event) {
var key = '';
this.data.show = {
index: false,
types: false,
course: false,
user: false
};
console.log(event)
switch (event.detail) {
case 0:
key = 'index';
Global\_Data = index\_js.getData();
break;
case 1:
key = 'types';
Global\_Data = types\_js.getData()
break;
case 2:
key = 'course';
Global\_Data = index\_js.getData();
break;
case 3:
key = 'user';
Global\_Data = index\_js.getData();
break;
}
this.data.show\[key\] = true;
console.log(Global\_Data)
this.setData({
show: this.data.show,
Global\_Data: Global\_Data
})
},
});
wxml entry file
css entry
@import “/template/user/user.wxss”;
.container { height: 100vh; background-color: #fff; }
.tag, .button { margin-right: 5px; }
.van-card__footer { margin-top: 5px; }
Then the code structure of the subpage js
const app = getApp(); var index_data = { banner: [ ‘//img4.mukewang.com/szimg/5c4a74c009dea3b500000000.jpg’, ‘//img2.mukewang.com/szimg/5c734d880939299918000600.jpg’, ‘//img4.mukewang.com/szimg/5c63e89209f9f17d00000000.jpg’ ], imageUrl: ‘http://img1.sycdn.imooc.com/szimg/5c6bdb3e08e4674a06000338-360-202.jpg', tabs_active: 0 }; // app.alert(‘aa’); // The interface needs to be open to the outside world, otherwise it cannot be called module.exports = {//Used to return global data getData: function() { return index_data; }, myfunction: function (){ app.alert(‘aa’); }
};
Just write css and wxml as usual. If the wxml needs to be reused, continue to separate the public part in the current directory and introduce it through the template tag.
This method can not only quickly switch pages without flickering, but also divide files and make them easy to manage.
This method was born by combining the failure experiences of the previous two methods and combining them into one.
Since the final project was not fully written, I was asked to develop other projects. There may be some omissions in the details. If there is any new progress in the future, I will update it.
