zoukankan      html  css  js  c++  java
  • 在小程序中使用状态管理mobx

    在小程序中使用状态管理mobx

    使用mobx后,进入二级页面修改数据返回一级页面显示的数据跟着变

    先看效果

    控制台打印的数据

    以下是代码

    store.js

    // js/store.js
    // 手动安装时引入的路径
    import {configure, extendObservable} from "./mobx";
    
    // 设置可在任何地方修改
    configure({
      enforceActions: 'never',
    });
    
    let Store = function () {
      /*属性*/
      extendObservable(this, {
        // observable data
        todoText: 'aaa',
        // computed data
        get todoTextCount() {
          return this.todoText.length;
        }
      });
    
      /*方法*/
      // action
      this.changeTodoText = function (todoText) {
        this.todoText = todoText;
      }
    }
    
    module.exports.store = new Store

    test.js

    // pages/test/test.js
    import { autorun } from '../../js/mobx';
    import { store } from '../../js/store';
    
    Page({
      data: {
        todoText: store.todoText,
      },
      // your other code below
      onLoad: function (evt) {
        this._autorun();
        const _this = this;
        console.log('onLoad', evt);
        // this._autorun();
        store.changeTodoText('test.store.111');
        store.todoText = 'test.store.222';
      },
      _disposer: [],
      _autorun: function () {
        const _this = this;
        _this._disposer.push(
          autorun(function () {
            console.log('autorun.todoText', _this.data, store);
            _this.setData({ todoText: store.todoText });
          })
        );
      },
      onUnload: function (evt) {
       this._disposer.map(function (x) { x() });
    this._disposer.length = 0; }, onJumpAaa: function (evt) { tt.navigateTo({ url: '/pages/aaa/aaa' // 指定页面的url }); } })

    aaa.js

    // pages/aaa/aaa.js
    import { autorun } from "../../js/mobx";
    import { store } from "../../js/store";
    
    Page({
      data: {
        todoText: store.todoText,
      },
      // your other code below
      onLoad: function (evt) {
        this._autorun();
        const _this = this;
        console.log('onLoad', evt);
        store.changeTodoText('aaa.store.111');
        store.todoText = 'aaa.store.222';
      },
      _disposer: [],
      _autorun: function () {
        const _this = this;
        _this._disposer.push(
          autorun(function () {
            console.log('autorun.todoText', _this.data, store);
            _this.setData({ todoText: store.todoText });
          })
        );
      },
      onUnload: function (evt) {
       this._disposer.map(function (x) { x() });
    this._disposer.length = 0; }, })

    本文链接:

    https://www.cnblogs.com/stumpx/p/13159450.html

  • 相关阅读:
    阿里巴巴Java开发规约扫描插件-Alibaba Java Coding Guidelines 在idea上安装使用教程
    [Rust] 数据类型的转换
    [golang] 错误处理
    [golang] 变量声明和初始化 var, :=, new() 和 make()
    [golang] 概念: struct vs interface
    [Rust] 变量的属性: 不可变(immutable), 可变(mutable), 重定义(shadowing), 常量(const), 静态(static)
    [Rust] 命名习惯
    [Rust] Workspace,Package, Crate 和 Module
    如何将 IPhone 的文件导入 Linux
    软引用和弱引用
  • 原文地址:https://www.cnblogs.com/stumpx/p/13159450.html
Copyright © 2011-2022 走看看