当前位置:   article > 正文

小程序 - Taro小程序中打开h5页面链接 - 并解决:无法打开该页面 - 不支持打开 https://www.baidu.com/,请在“小程序右上角更多->反馈与投诉”中和开发者反馈_请在“小程序右上角更多->反馈与投诉”中和开发者反馈。

请在“小程序右上角更多->反馈与投诉”中和开发者反馈。

业务需求

Taro 小程序中h5跳转打开页面

解决方案

借助 webView 组件打开h5页面

实现

1. 使用 WebView 组件创建浏览h5的页面

@/pages/webView/webView.js 文件

import Taro, { Component } from '@tarojs/taro'
import { WebView } from '@tarojs/components'
class WebviewDetail extends Component {
  config = {
    navigationBarTitleText: ''
  }
  componentDidMount() {
  }
  render() {
    return (
      <WebView src={decodeURIComponent(this.$router.params.targetUrl)}></WebView>
    )
  }
}
export default WebviewDetail
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Taro 中 webView 组件

2. app.js 中配置h5页面路径

29 行 主要代码

/* eslint-disable react/sort-comp */
/* eslint-disable import/first */
import '@tarojs/async-await'
import Taro, { Component } from '@tarojs/taro'
import { Provider } from '@tarojs/redux'
import websocket from './utils/websocket'
import * as websocketActions from '@actions/websocket'
import chat from './utils/chat'
import sitemap from './sitemap.json'
import Index from './pages/index'
import './utils/config'
import '@utils/indexConfig'
import configStore from './store'
import 'taro-ui/dist/style/index.scss'
import './app.scss'

if (process.env.TARO_ENV !== 'alipay') {
  require('@tarojs/async-await')
}

const store = configStore()

class App extends Component {
  config = {
    pages: [
      'pages/index/index',
      'pages/index/history/history',
      'pages/index/history/search',
      'pages/webView/webView', // 【主要代码】
    ],
    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: 'WeChat',
      navigationBarTextStyle: 'black',
    },
    "permission": {
      "scope.userLocation": {
        "desc": "你的位置信息将用于小程序位置接口的效果展示" // 高速公路行驶持续后台定位
      }
    },
    "plugins": {
      "WechatSI": {
        ...
      }
    },
    "sitemapLocation": sitemap,
    "navigateToMiniProgramAppIdList": [],
  }
  componentDidMount () {
    chat.registerUpdateManagerCallback();

    websocket.setSocketTaskId(0);
    websocket.checkNetWork();//初始化连接,检查网络状态
    websocket.clearSocketTimer();
    chat.showMsg()
    websocket.heartBeatTask();
    websocket.registerRecordManagerCallback();//语音
  }
  componentDidShow () {
    if(!Taro.getStorageSync('hasWelcome')){
      Taro.setStorage({ key:'hasWelcome',data:global.indexData.hasWelcome })
    }
  }
  componentDidHide () {
  }

  componentWillUnmount(){
    store.dispatch(websocketActions.dispatchIsLoginUpload(false));
  }

  componentCatchError () {}

  componentDidCatchError () {}

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render () {
    return (
      <Provider store={store}>
        <Index />
      </Provider>
    )
  }
}

Taro.render(<App />, document.getElementById('app'))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

3. 实现跳转

Taro.navigateTo({
  url: '../webView/webView?targetUrl=' + encodeURIComponent(e.href), // e.href 为要跳转的h5地址,如 https://www.baidu.com/
})
  • 1
  • 2
  • 3

4. 遇到的问题

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/560179
推荐阅读
相关标签
  

闽ICP备14008679号