What is the significance of Mini Program's wxs module?

problem description

what is the purpose of the existence of the wxs module? What"s the difference between a normal js module and a normal one? What is the meaning of using variables and functions defined in the wxs module in wxml?

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?


The purpose of

wxs is to act as a function such as compute/filter. There is watch in
vue (filter (filter) in the listening property), angular). Its core function is to avoid repeatedly writing excessively long expressions in the template.

We always encounter the need to format numbers as amounts, or to replace placeholders with corresponding internationalized content, or to automatically select the smallest value from multiple values for output. If the expression is written directly in the template, it is not conducive to later maintenance, and it is impossible to reuse the expression. With the filter, we can extract this complex statement separately and then reference it in the template.

add:
Why not write the result directly to data?
because the common scenario for filters is to do presentation layer logic. For example, if you convert the number to the amount format mentioned above, you only need to call the filter where it is used in the template to convert the data into the final format you need.
if you use data, you need to format and write back the original data at every place where it is possible to modify it. For the example of amount, you need to iterate over the properties that need to be converted after each interface returns, change it to the amount format, and then save it into a new field. The workload is huge, maintenance is difficult, and it is easy to miss.

Menu