小程序全局变量的应用

为了方便小程序页面件的数据传输,需要将变量写入到全局变量,特地封装成了3个全局函数

  • 登录信息函数
  • App(全局变量,变量写入全局变量并写入缓存,不随着页面关闭而销毁)
  • G(全局变量,用于页面件临时数据的传输,页面关闭后自动销毁)

#登录信息函数
用于存储用户的登录信息,用户不退出信息不销毁

  • hasLogin 用户的登录状态
  • skey 远程操作数据库的密钥
  • userInfo 用户的信息资料
  • user_config 用户页面其他常用变量设置

##操作方法

//加载全局变量
import {
            mapState,
            mapMutations
} from 'vuex'

//
computed: {
        //引入全局变量
        ...mapState({
            forcedLogin: state => state.forcedLogin,
            hasLogin: state => state.hasLogin,
            userinfo: state => state.userInfo,
            skey: state => state.skey,
            user_config: state => state.user_config,
        })
},
methods:  Object.assign({
        //引入函数操作
        ...mapMutations(['login','logout'])


},Page.Fs),

##函数介绍

函数名 功能 操作
login 登录 this.login(‘登录信息’)
logout 退出 this.logout()
islogin 判断是否登录 this.islogin()

#App

全局变量,变量写入全局变量并写入缓存,不随着页面关闭而销毁.

主要用于长时间不需要更新的内容,并经常需要调用的数据,如关于我们,网站名称,LOGO地址,用户的设置等信息

##操作方法

//加载全局变量
import {
            mapState,
            mapMutations
} from 'vuex'

//
computed: {
        //引入全局变量
        ...mapState({P: state => state.App}),
},
methods:  Object.assign({
        //引入函数操作
        ...mapMutations(['setCache'])


},Page.Fs),

##函数介绍

函数名 功能 操作
setCache 将变量写入App this.setCache(array)

array

this.setCache({
            name:'web_site',
            def:{
                value:977,
                s:28666
            }
        })

函数名|功能|操作
—|—|—
name|定义变量的名称|web_site
def|定义变量的内容

##调用方法

//js
this.P.web_site.value
//模板
{{P.web_site.value}}

#G

全局变量,用于页面件临时数据的传输,页面关闭后自动销毁

主要用于页面间的数据传递,用于页面件的数据传输和回调

##操作方法

//加载全局变量
import {
            mapState,
            mapMutations
} from 'vuex'

//
computed: {
        //引入全局变量
        ...mapState({G: state => state.G}),
},
methods:  Object.assign({
        //引入函数操作
        ...mapMutations(['setG'])


},Page.Fs),

##函数介绍

函数名 功能 操作
setG 将变量写入App this.setG(array)

array

this.setG({
            name:'web_site',
            def:{
                value:977,
                s:28666
            }
        })
函数名 功能 操作
name 定义变量的名称 web_site
def 定义变量的内容

##调用方法

//js
this.G.web_site.value
//模板
{{G.web_site.value}}

发表评论

电子邮件地址不会被公开。 必填项已用*标注