[英]Getting different const values from a stateless component
我是反应原生的新手。 抱歉,如果我的问题看起来很愚蠢。 我想在文件中写入每个http url并在需要的地方访问它们。 我怎样才能做到这一点?
例如,我是AllUrl.js文件,我想在其中写下以下网址
const baseUrl = "https://amrita.com/";
const secondaryUrl = "https://google.com";
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
然后我想在需要的地方访问单个url字符串。
例如:
import AllUrls from "./AllUrls";
export default class Home extends React.Component {
componentWillMount() {
fetch(AllUrls.baseUrl) //I want to do this ie. call baseUrl const here
.then(response => response.json())
.then(responseJson => {
this.setState({
homeList: responseJson.homeLIst,
});
})
.catch(error => {
alert(error);
});
}
render(){
return(
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
);
}
}
我该怎么做? 如何从AllUrl.js访问baseUrl字符串到Home.js
如果要从其他文件导入内容,则必须导出它们。
例如:在AllUrl.js文件中。
export const baseUrl = "https://amrita.com/";
export const secondaryUrl = "https://google.com";
然后将它们导入另一个文件
import {baseUrl, secondaryUrl} from "./AllUrls";
export default class Home extends React.Component {}
我建议您在使用React Native制作移动应用程序之前,应该花点时间学习Javascript语法。 上面的代码是ES5 / ES6 Javscript。 :))
欢呼!
根据您的情况,您需要将所有variables
导出为default
const baseUrl = "https://amrita.com/";
const secondaryUrl = "https://google.com";
export default {
baseUrl,
secondaryUrl
}
并将它们用作
import AllUrls from "./AllUrls";
AllUrls.baseUrl
AllUrls.secondaryUrl
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.