2017-03-17 91 views
2

我嘗試在本地.html文件加載到WebView陣營本地(陣營母語)加載本地HTML文件到的WebView

// load local .html file 
const PolicyHTML = require('./Policy.html'); 

// PolicyHTML is just a number of `1` 
console.log('PolicyHTML:', PolicyHTML); 

// will cause an error: JSON value '1' of type NSNumber cannot be converted to NSString 
<WebView html={PolicyHTML} /> 

.html文件應被理解爲一個字符串,而不是作爲一種資源代表。

如何將.html文件加載到WebView的React Native中?


順便說一句,那些來自require()的資源代表的類型是什麼?是number

+0

您是否找到了解決方案?請更新正確答案。 – Rai

+0

@Rai還沒有。 :( –

回答

6

嘗試:

const PolicyHTML = require('./Policy.html'); 

<WebView 
    source={PolicyHTML} 
    style={{flex: 1}} 
/> 
+0

您能解釋一下這段代碼的功能嗎,或者它爲什麼起作用? –

+0

它可以在開發環境中工作。在發行版本中,請點擊此鏈接:https://github.com/facebook/react-native/issues/505 – Fmussap

+0

@Fmussap,嗯,但我在iOS發行版上使用這個解決方案,它的工作原理是 –

0

試試這個:

  • 添加您的.html文件在您的項目。代碼
  • 寫這樣的文件中的行要使用的WebView組件

    const OurStoryHTML = require ('./OurStory.html')

  • <WebView source={OurStoryHTML} style={{flex: 1, marginTop : 44}} />

它可以幫助你。

0
<View style={{flex: 1}}> 
    <WebView 
     style={{flex: 1}} 
     source={require("./resources/index.html")} 
    /> 
</View> 

要製作WebView,父級必須具有維度或flex:1。我們可以將WebView設置爲flex:1,以填充父項。