Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否实现组件与组件的映射功能 #2212

Closed
tourze opened this issue Feb 19, 2019 · 3 comments
Closed

能否实现组件与组件的映射功能 #2212

tourze opened this issue Feb 19, 2019 · 3 comments
Labels
question Further information is requested

Comments

@tourze
Copy link
Contributor

tourze commented Feb 19, 2019

问题描述
希望有能力在代码里面实现 “源组件” 和 “目标组件” 的映射。

期望行为
举个例子。好比我们在app开发中会用到WebView组件。小程序、RN都有对应的组件去做渲染,但是H5没有这种能力的,所以我们希望在打开H5中使用WebView组件,实则渲染的是一个WebRedirect组件,用于提示用户“即将跳转到 http://xxxxx”。

在app.js中可以使用类似这样的实现:

import Taro from '@tarojs/taro'
import { WebView } from '@tarojs/components'
import WebRedirect from './components/WebRedirect'

if (process.env.TARO_ENV === 'h5') {
    Taro.componentMapping = {
        WebView: WebRedirect
    };
}

...

然后在具体组件中使用 import { WebView } from '@tarojs/components',就自动换成WebRedirect组件

系统信息
👽 Taro v1.2.13

Taro CLI 1.2.13 environment info:
System:
OS: macOS 10.14.3
Shell: 5.3 - /bin/zsh
Binaries:
Node: 11.9.0 - /usr/local/bin/node
Yarn: 1.13.0 - /usr/local/bin/yarn
npm: 6.5.0 - /usr/local/bin/npm
npmGlobalPackages:
typescript: 2.4.2

@taro-bot
Copy link

taro-bot bot commented Feb 19, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@luckyadam
Copy link
Member

其实可以转换一下思路
你可以创建一个 MyWebView 组件,如下

import Taro, { Component } from '@tarojs/taro'
import { WebView } from '@tarojs/components'
import WebRedirect from './components/WebRedirect'

export default class MyWebView extends Component {
  render () {
    return (
      process.env.TARO_ENV === 'h5' ? <WebRedirect /> : <WebView />
    )
  }
}

然后对外统一使用 MyWebView 组件就行

@luckyadam luckyadam added question Further information is requested answered labels Feb 21, 2019
@taro-bot
Copy link

taro-bot bot commented Feb 21, 2019

Hello~

您的问题楼上已经有了确切的回答,如果没有更多的问题这个 issue 将在 15 天后被自动关闭。

如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。

Good luck and happy coding~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants