React Native:如何使用动态网址的require(路径)? [英] React Native: how to use require(path) with dynamic urls?

查看:506
本文介绍了React Native:如何使用动态网址的require(路径)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用WebView显示一些html内容

I want to use WebView to show some html content

这是一个例子:

return (
            <WebView
                    style={styles.container}
                    source={source}
                    scalesPageToFit={Boolean(true)}
                    onNavigationStateChange={this._onNavigationStateChange} />
    )

source 变量我需要有两个不同的值:

and for the source variable I need to have two different values:

1)对于android平台我需要使用这样的东西:

1) for android platform I need to use something like this:

source = {uri: `file:///android_asset/contents/${languageId}text.html`}

2)对于ios我需要使用smth。像这样:

2) for ios I need to use smth. like this:

source = require(`../srv/localization/contents/${languageId}text.html`)

对于android它运作良好,但对于ios它不起作用。
此网址适用于iOS也适用

For android it works well, but for ios it doesn't work. And this url works fine for iOS also

require(`../srv/localization/contents/entext.html`)

据我所知,这是因为动态网址($ {languageId} text.html)

As I understand that is because of dynamic url (${languageId}text.html)

问题是如何在iOS上使用动态网址?

The question is how to use dynamic urls for iOS?

推荐答案

正如您所知, require 不能拥有动态 url 。这是因为 require 在应用程序启动时获取源代码,无论它在代码中的位置。你需要所有的 {languageId} text.html 并将所需的变量传递给源:

As you find out, you can't have dynamic url for require. That's because require get the source at the app start regardless it's place in the code. You shuld require all of the {languageId}text.html and pass the required variable to the source:

var language = {
   en: require(`../srv/localization/contents/entext.html`)
   ...
}

并使用如下:

source = require(language[en])

这篇关于React Native:如何使用动态网址的require(路径)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆