webview.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import Taro, { Component } from '@tarojs/taro'
  2. import { View, WebView } from '@tarojs/components'
  3. /**
  4. * // NOTE Taro 的 RN 端还未提供 WebView 组件,可以引入原生组件来解决
  5. * (备注:Taro v1.2.16 已支持,这块代码还是保留作为演示)
  6. *
  7. * Taro 有开启了 tree shaking,对于未用到的内容编译时会自动去除
  8. * 因此可以把相应端的内容都 import 进来,再根据环境进行调用即可
  9. *
  10. * 另外 1.2.17 版本有提供了统一接口方式 https://nervjs.github.io/taro/docs/envs.html
  11. * 可以参考 ./src/pages/user-login 中的实现
  12. */
  13. import WebViewRN from './rn'
  14. import './webview.scss'
  15. export default class extends Component {
  16. config = {
  17. navigationBarTitleText: ''
  18. }
  19. url = ''
  20. title = ''
  21. componentWillMount() {
  22. this.url = decodeURIComponent(this.$router.params.url || '')
  23. this.title = decodeURIComponent(this.$router.params.title || '')
  24. Taro.setNavigationBarTitle({ title: this.title })
  25. }
  26. render () {
  27. return (
  28. <View className='webview'>
  29. {/* // NOTE 编译时只会保留相应端的内容,因此非 RN 端时不会编译 WebViewRN */}
  30. {process.env.TARO_ENV === 'rn' ?
  31. <WebViewRN src={this.url} /> :
  32. <WebView src={this.url} />
  33. }
  34. </View>
  35. )
  36. }
  37. }