react-native全局变量和静态变量使用

react-native 多个类引入变量,可以使用下面三个方式

静态变量
全局变量的使用:
1 导入文件方式
2 全局变量方式

实现类

Test.js

import {Button, Text, View} from 'react-native';
import React, {Component} from 'react';
// import global from './Global'
import './Global.js';

export default class TabOne extends Component{

    static staticdefine = 123;

    constructor(props){
        super(props);
    }

    render(){

        return (<View style={{padding:20}}>
            <Text>静态变量:{TabOne.staticdefine}</Text>

            <Button title="modify static variable"
                    onPress={this.actionButton}
                    color={'#00f'}/>


            {/*<Text>{global.name}</Text>*/}
            {/*<Text>{global.website}</Text>*/}

            {/*<Button title="modify The global variable "*/}
            {/*        onPress={this.modifyVariableButton}*/}
            {/*        color={'#00f'}/>*/}


            <Text>{global.variables.authorization}</Text>
            <Text>{global.variables.language}</Text>
            <Button title="modify The global variable2 "
                    onPress={this.modifyVariableButton2}
                    color={'#00f'}/>


        </View>);
    }

    actionButton = () => {
        TabOne.staticdefine=321
        alert( TabOne.staticdefine)
    }

    modifyVariableButton = () => {
        global.name=12;
        alert(global.name)
    }

    modifyVariableButton2 = () => {
        global.variables.language='chinese';
        alert(global.variables.language)
    }
}

global.js


const object = {
    website:'http://www.baidu.com',
    name:'百度',
};
export default object;

// global.constants = {
//     website:'http://www.baidu.com2',
//     name:'百度2',
// };


global.variables = {authorization: null,language: 'English'}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值