zoukankan      html  css  js  c++  java
  • Taro -- 定义全局变量

    Taro定义全局变量

    方法1:在taro中 getApp()只能取到一开始定义的值,并不能取到改变后的值

    // app.js文件中
    class App extends Component {
      constructor () {
        super(...arguments)
        this.state = ({
          
        })
      }
      globalData = {
        loginType: false,
        userInfo: {}
      }
    }
    // index.js 文件中
    const app = Taro.getApp();
    export default class Index extends Component {
      componentWillMount() {
        console.log(app.globalData.loginType);
      }
    }

    方法2:新增一个自行命名的 JS 文件,例如 global.js

    const globalData = {}
    export function setGlobalData (key, val) {
      globalData[key] = val
    }
    export function getGlobalData (key) {
      return globalData[key]
    }

    然后在需要改变传值的app.js页面调用,存入数据

    import {setGlobalData} from "./global"
    setGlobalData("loginType", true)  //全局变量

    最后在需要的index.js页面取出数据

    import {getGlobalData} from "../../global"
    componentDidShow () {
         console.log(getGlobalData("loginType"))
    }

    方法3:使用 Redux 来进行全局变量的管理

  • 相关阅读:
    leetcode刷题29
    leetcode刷题28
    leetcode刷题27
    leetcode刷题23
    leetcode刷题22
    leetcode刷题21
    leetcode刷题20
    Unity中通过DoTween实现转盘效果
    U3D工作注意事项,不要再犯!
    Unity中String字符串的优化
  • 原文地址:https://www.cnblogs.com/juewuzhe/p/11097146.html
Copyright © 2011-2022 走看看