Javascript 反应自然—;全局变量不变
我有两个独立的文件,第一个是组件(List.js),它使用第二个(APIService.js)获取不同的API。要更正获取,URL需要接收全局变量。现在,我正试图从APIService文件中的函数重新定义这些变量,但没有成功。就在Javascript 反应自然—;全局变量不变,javascript,api,variables,react-native,Javascript,Api,Variables,React Native,我有两个独立的文件,第一个是组件(List.js),它使用第二个(APIService.js)获取不同的API。要更正获取,URL需要接收全局变量。现在,我正试图从APIService文件中的函数重新定义这些变量,但没有成功。就在API调用注释之前,APIService.js中重新定义了变量 我有两个问题: 为什么全局变量naptanId没有被重新定义 是否可以从组件定义和传递这些变量 伪代码 探测信标 重新定义naptanId 使用最近定义的变量获取组件API API调用已完成 数据被传
API调用
注释之前,APIService.js
中重新定义了变量
我有两个问题:
- 为什么全局变量
没有被重新定义naptanId
- 是否可以从组件定义和传递这些变量
- 探测信标
- 重新定义naptanId
- 使用最近定义的变量获取组件API
- API调用已完成
- 数据被传递回组件
- 设定状态
componentDidMount() {
// Executes first function
APIService._fetchStopPoint((resp1) => {
console.log("Stoppoint", resp1)
// ... and set the bus state with the first response
this.setState({
bus: resp1
});
// ... based on the response, convert array to string
const lines = (resp1.lines.map((line) => line.name)).toString()
// ... pass lines to sencond function
APIService._fetchArrivalTimes(lines, (resp2) => {
// .. and set the tube state with the second response
this.setState({
isLoading: false,
tube: resp2
});
});
});
}
// Variables
// ***********************************************************************
let naptanId = undefined
let lines = undefined
let ice = '59333'
let mint = '57011'
let blueberry = '27686'
let nearestBeacon = undefined;
let newBeaconId = undefined;
let setIce = false;
let setBlueberry = false;
let setMint = false;
// Beacon detection
// ***********************************************************************
const region = {
identifier: 'Estimotes',
uuid: '354A97D8-9CAF-0DC7-CE0E-02352EBE90CD',
};
// Request for authorization while the app is open
Beacons.requestWhenInUseAuthorization();
Beacons.startMonitoringForRegion(region);
Beacons.startRangingBeaconsInRegion(region);
Beacons.startUpdatingLocation();
// Listen for beacon changes
const subscription = DeviceEventEmitter.addListener('beaconsDidRange', (data) => {
const ibeacons = data.beacons
// var lowestAccuracySeen = 0.5;
let lowestAccuracySeen = "immediate"
// Check if beacons are updating
if (ibeacons && ibeacons.length > 0) {
// Loop through beacons array
for (var i = 0; i < ibeacons.length ; i++) {
// Find beacons with same minor ...
var foundBeacon = ibeacons.find(function(closestBeacon) {
// ... and return the beacon the lowest accuracy seen
// return closestBeacon.accuracy.toFixed(2) < lowestAccuracySeen;
return closestBeacon.proximity == lowestAccuracySeen
});
// If found ...
if (foundBeacon) {
// ... define the lowest accuracy and the nearest beacon
lowestAccuracySeen = foundBeacon.accuracy;
nearestBeacon = foundBeacon;
// Identify what component to render against nearest beacon
setIce = nearestBeacon.minor == ice ? true : false;
setMint = nearestBeacon.minor == mint ? true : false;
setBlueberry = nearestBeacon.minor == blueberry ? true : false;
if (setIce) {
// THESE VARIABLES CANNOT BE REDEFINED
naptanId = "490004936E"
lines = "55"
} else if (setMint) {
} else if (setBlueberry) {
};
}
}
}
});
// API calls
// ***********************************************************************
class APIService {
// Fecth stop point info
static _fetchStopPoint(cb) {
console.log(naptanId, lines)
fetch(`https://api.tfl.gov.uk/StopPoint/${naptanId}`)
.then(stopData => {
try {
stopData = JSON.parse(stopData._bodyText); // Converts data to a readable format
cb(stopData, naptanId);
} catch(e) {
cb(e);
}
})
.catch(e => cb(e));
}
// Fetch arrival times info
static _fetchArrivalTimes(lines, cb) {
fetch(`https://api.tfl.gov.uk/Line/${lines}/Arrivals/${naptanId}`)
.then(arrivalData => {
try {
arrivalData = JSON.parse(arrivalData._bodyText);
arrivalTime = arrivalData
cb(arrivalData);
} catch(e) {
cb(e);
}
})
.catch(e => cb(e));
}
// Fetch status info
static _fetchStatus(lines) {
fetch(`https://api-argon.digital.tfl.gov.uk/Line/${lines}/Status`)
.then(statusData => {
try {
statusData = JSON.parse(statusData._bodyText); // Converts data to a readable format
cb(statusData);
} catch(e) {
cb(e);
}
})
.catch(e => cb(e));
}
}
module.exports = APIService;
APIService.js
componentDidMount() {
// Executes first function
APIService._fetchStopPoint((resp1) => {
console.log("Stoppoint", resp1)
// ... and set the bus state with the first response
this.setState({
bus: resp1
});
// ... based on the response, convert array to string
const lines = (resp1.lines.map((line) => line.name)).toString()
// ... pass lines to sencond function
APIService._fetchArrivalTimes(lines, (resp2) => {
// .. and set the tube state with the second response
this.setState({
isLoading: false,
tube: resp2
});
});
});
}
// Variables
// ***********************************************************************
let naptanId = undefined
let lines = undefined
let ice = '59333'
let mint = '57011'
let blueberry = '27686'
let nearestBeacon = undefined;
let newBeaconId = undefined;
let setIce = false;
let setBlueberry = false;
let setMint = false;
// Beacon detection
// ***********************************************************************
const region = {
identifier: 'Estimotes',
uuid: '354A97D8-9CAF-0DC7-CE0E-02352EBE90CD',
};
// Request for authorization while the app is open
Beacons.requestWhenInUseAuthorization();
Beacons.startMonitoringForRegion(region);
Beacons.startRangingBeaconsInRegion(region);
Beacons.startUpdatingLocation();
// Listen for beacon changes
const subscription = DeviceEventEmitter.addListener('beaconsDidRange', (data) => {
const ibeacons = data.beacons
// var lowestAccuracySeen = 0.5;
let lowestAccuracySeen = "immediate"
// Check if beacons are updating
if (ibeacons && ibeacons.length > 0) {
// Loop through beacons array
for (var i = 0; i < ibeacons.length ; i++) {
// Find beacons with same minor ...
var foundBeacon = ibeacons.find(function(closestBeacon) {
// ... and return the beacon the lowest accuracy seen
// return closestBeacon.accuracy.toFixed(2) < lowestAccuracySeen;
return closestBeacon.proximity == lowestAccuracySeen
});
// If found ...
if (foundBeacon) {
// ... define the lowest accuracy and the nearest beacon
lowestAccuracySeen = foundBeacon.accuracy;
nearestBeacon = foundBeacon;
// Identify what component to render against nearest beacon
setIce = nearestBeacon.minor == ice ? true : false;
setMint = nearestBeacon.minor == mint ? true : false;
setBlueberry = nearestBeacon.minor == blueberry ? true : false;
if (setIce) {
// THESE VARIABLES CANNOT BE REDEFINED
naptanId = "490004936E"
lines = "55"
} else if (setMint) {
} else if (setBlueberry) {
};
}
}
}
});
// API calls
// ***********************************************************************
class APIService {
// Fecth stop point info
static _fetchStopPoint(cb) {
console.log(naptanId, lines)
fetch(`https://api.tfl.gov.uk/StopPoint/${naptanId}`)
.then(stopData => {
try {
stopData = JSON.parse(stopData._bodyText); // Converts data to a readable format
cb(stopData, naptanId);
} catch(e) {
cb(e);
}
})
.catch(e => cb(e));
}
// Fetch arrival times info
static _fetchArrivalTimes(lines, cb) {
fetch(`https://api.tfl.gov.uk/Line/${lines}/Arrivals/${naptanId}`)
.then(arrivalData => {
try {
arrivalData = JSON.parse(arrivalData._bodyText);
arrivalTime = arrivalData
cb(arrivalData);
} catch(e) {
cb(e);
}
})
.catch(e => cb(e));
}
// Fetch status info
static _fetchStatus(lines) {
fetch(`https://api-argon.digital.tfl.gov.uk/Line/${lines}/Status`)
.then(statusData => {
try {
statusData = JSON.parse(statusData._bodyText); // Converts data to a readable format
cb(statusData);
} catch(e) {
cb(e);
}
})
.catch(e => cb(e));
}
}
module.exports = APIService;
//变量
// ***********************************************************************
设naptanId=未定义
让线=未定义
让冰='59333'
let mint='57011'
让蓝莓='27686'
设最近信标=未定义;
设newBeaconId=未定义;
让setIce=false;
让我们假设你是错的;
设setMint=false;
//信标检测
// ***********************************************************************
常量区域={
标识符:“估计值”,
uuid:'354A97D8-9CAF-0DC7-CE0E-02352EBE90CD',
};
//应用程序打开时请求授权
Beacons.RequestWhenUseAuthorization();
信标。开始监视区域(区域);
信标。星载信标区域(区域);
信标。startUpdatingLocation();
//倾听信号灯的变化
const subscription=DeviceEventEmitter.addListener('beaconsDidRange',(数据)=>{
const ibeacons=data.beacons
//var Lowestaccuracysee=0.5;
let Lowestaccuracyseed=“立即”
//检查信标是否正在更新
if(ibeacons&&ibeacons.length>0){
//环通信标阵列
对于(var i=0;i{
试一试{
stopData=JSON.parse(stopData._bodyText);//将数据转换为可读格式
cb(stopData,naptanId);
}捕获(e){
cb(e);
}
})
.catch(e=>cb(e));
}
//获取到达时间信息
静态到达时间(行、cb){
取回(`https://api.tfl.gov.uk/Line/${lines}/Arrivals/${naptanId}`)
。然后(arrivalData=>{
试一试{
arrivalData=JSON.parse(arrivalData.\u bodyText);
到达时间=到达数据
cb(到达数据);
}捕获(e){
cb(e);
}
})
.catch(e=>cb(e));
}
//获取状态信息
静态_获取状态(行){
取回(`https://api-argon.digital.tfl.gov.uk/Line/${lines}/Status`)
。然后(statusData=>{
试一试{
statusData=JSON.parse(statusData.\u bodyText);//将数据转换为可读格式
cb(状态数据);
}捕获(e){
cb(e);
}
})
.catch(e=>cb(e));
}
}
module.exports=APIService;
处理这些全局变量(跨不同组件)的最简单方法是使用:
对于更多性能关键的全球VAR,您也可以考虑(如CORIDATA、SQLite在IOS和Android中)。戴维:谢谢您的回复。我不熟悉AsyncStorage,但我会尝试一下。我还可以将AsyncStorage从组件传递到类吗?假设我在
List.js
组件上定义了AsyncStorage,但是我想在APIService.js
中从类APIService
中检索它。可能吗?