当前位置:   article > 正文

基于TypeScript类Class开发微信小程序页面Page_typescript onload option

typescript onload option

之前也尝试基于ts class开发小程序页面,失败了,今天中午不死心,又试了下,摸索出一种方式,大家稍微参考下,主要是想遍历new出来的对象所有属性,再构建一个var obj = {}字面对象:

构造函数似乎用不了,回头再研究研究,目前这样,已经很惊喜了,哈哈!

具体还有什么坑,后面发现了再更新上来。

  1. // import { IMyApp } from "../../app";
  2. import { AppServiceProvider } from "../../providers/app-service/app-service";
  3. import { AlertServiceProvider } from "../../providers/alert-service/alert-service";
  4. import { WxServiceProvider } from "../../providers/wx-service/wx-service";
  5. import { WxBindRes } from "../../providers/constants";
  6. export class MallPage{
  7. data = {
  8. showBoxIndex : 1
  9. }
  10. // constructor(public appService: AppServiceProvider) { }
  11. /**
  12. * 生命周期函数--监听页面加载
  13. * options: any
  14. */
  15. onLoad(options: any) {
  16. console.log(options);
  17. wxService.setPageTitle("首页");
  18. }
  19. /**
  20. * 页面跳转
  21. */
  22. toPage(e: WxBindRes) {
  23. appService.push(e.currentTarget.dataset.page);
  24. }
  25. /**
  26. * 显示alert
  27. */
  28. showAlert() {
  29. alertService.alert("提示信息");
  30. }
  31. }
  32. // 关键代码如下
  33. const page:any = new MallPage();
  34. const pageObj:any = {};
  35. for (let prop of Object.getOwnPropertyNames(page)) {
  36. pageObj[prop] = page[prop];
  37. }
  38. const ps:any = MallPage.prototype;
  39. for (let prop of Object.getOwnPropertyNames(ps)) {
  40. if (prop !== 'constructor'){
  41. pageObj[prop] = ps[prop];
  42. }
  43. }
  44. Page(pageObj);

20191106

继续优化,data:

  1. // import { IMyApp } from "../../app";
  2. import { AppServiceProvider } from "../../providers/app-service/app-service";
  3. import { AlertServiceProvider } from "../../providers/alert-service/alert-service";
  4. import { WxServiceProvider } from "../../providers/wx-service/wx-service";
  5. import { WxBindRes } from "../../providers/constants";
  6. const appService = new AppServiceProvider();
  7. const alertService = new AlertServiceProvider();
  8. const wxService = new WxServiceProvider();
  9. export class MallPage {
  10. showBoxIndex = 1;
  11. user:{name:string} = {name:'ty'};
  12. // constructor(public appService: AppServiceProvider) { }
  13. /**
  14. * 生命周期函数--监听页面加载
  15. * options: any
  16. */
  17. onLoad(options: any) {
  18. console.log(options);
  19. wxService.setPageTitle("首页");
  20. }
  21. /**
  22. * 页面跳转
  23. */
  24. toPage(e: WxBindRes) {
  25. appService.push(e.currentTarget.dataset.page);
  26. }
  27. /**
  28. * 显示alert
  29. */
  30. showAlert() {
  31. alertService.alert("提示信息");
  32. }
  33. }
  34. // 关键代码如下
  35. const page: any = new MallPage();
  36. const pageObj: any = {
  37. data:{}
  38. };
  39. for (let prop of Object.getOwnPropertyNames(page)) {
  40. pageObj.data[prop] = page[prop];
  41. }
  42. const ps: any = MallPage.prototype;
  43. for (let prop of Object.getOwnPropertyNames(ps)) {
  44. if (prop !== 'constructor') {
  45. pageObj[prop] = ps[prop];
  46. }
  47. }
  48. Page(pageObj);
  1. <view class="container">
  2. <view class="page-body">
  3. <button bindtap="toPage" data-page="../login/login">跳转登录{{showBoxIndex}}</button>
  4. <button bindtap="showAlert">显示弹框{{user.name}}</button>
  5. </view>
  6. </view>

杯具,setData不生效!

  1. changeName(){
  2. const that:any = this;
  3. // 无效!
  4. // this.user.name = 'zhl'
  5. // that.setData!(this.user);
  6. // 有效!
  7. that.setData!({user:{name:'zhl'}});
  8. // 有效!
  9. this.user.name = 'zhl'
  10. that.setData!({user:this.user});
  11. }

20191111

直接使用“赋值”的方式创建方法:

  1. //...
  2. const appService = new AppServiceProvider();
  3. const alertService = new AlertServiceProvider();
  4. const wxService = new WxServiceProvider();
  5. class HomePage{
  6. data = {
  7. name:'ty'
  8. }
  9. /**
  10. * 生命周期函数--监听页面加载
  11. * options: any
  12. */
  13. onLoad = function(options: any) {
  14. console.log(options);
  15. wxService.setPageTitle("首页");
  16. }
  17. /**
  18. * 页面跳转
  19. */
  20. toPage = function(e: WxBindRes) {
  21. appService.push(e.currentTarget.dataset.page);
  22. }
  23. /**
  24. * 显示alert
  25. */
  26. showAlert = function() {
  27. alertService.alert("提示信息");
  28. }
  29. }
  30. Page(new HomePage());

20200212

最近非常时期,在家办公快两周了,这几天公司小程序项目又多了起来,so...

无意中看到一个小程序开源项目,里面支持类的写法就是直接 new ,当时我就纳闷了,怎么我当年new的时候不行咧!

后来仔细看了下人家的源代码,发现一个惊天秘密,tsconfig.json配置的编译参数有差异,我配置的targetES6,而人家配置的是es5测试后发现果真是这个问题,配置改成es5之后,直接new就生效了,编译后的js直接操作了prototype(至今对es5、es6还是很马虎。。。):

  1. // tsconfig.json
  2. {
  3. "compilerOptions": {
  4. "module": "CommonJS",
  5. "target": "ES6",
  6. }
  7. }
  8. // 改为:
  9. {
  10. "compilerOptions": {
  11. "module": "CommonJS",
  12. "target": "es5",
  13. }
  14. }
  15. // page
  16. export class IndexPage {
  17. data = {
  18. res:'target改成es5即可!!!'
  19. }
  20. onLoad(options: any) {
  21. console.log(options);
  22. }
  23. showAlert() {
  24. }
  25. }
  26. Page(new IndexPage());

data的写法还不是我最终想要的,我想要的是不要data,可以配合上面20191106更新的data优化。

20200730 1:18

最近换了台电脑,重新装了下开发环境,所以新建了一个TS小程序工程,看看有没有什么新套路,果然有惊喜:

可以直接用类写页面了: 

20200810

最近又开始搞前后端框架升级,移动端跨端方案估计还是得找一套大厂现成的,京东的taro是首选,uniapp之前也用过了。

小程序官方这套,对于单独开发小程序一端来说,完全没问题。现在感觉比较烦的还是setData,之前也尝试过监听赋值事件,自动setData,后来没深入弄下去,今天在网上看到一段代码,突然又回想起来这个:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/797227
推荐阅读
相关标签
  

闽ICP备14008679号