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

pages目录下 @connect 里面如何获取路由参数,在@connect里面的props是空的 #2607

Closed
dxhuii opened this issue Mar 28, 2019 · 3 comments
Assignees

Comments

@dxhuii
Copy link

dxhuii commented Mar 28, 2019

问题描述
如路由是 /play/subject/index?id=1
在redux里的 @connect 这里想获取 id=1 现在是获取不到的

这样做,是为了,把每一个id存一个KEY,方便缓存数据。现在取不到了,就很麻烦。

@connect(
  (state, props) => ({
    play: getPlayList(state, props.match.params.id)
  }),
  dispatch => ({
    playlist: bindActionCreators(playlist, dispatch)
  })
)
class PlayList extends Component {
 componentDidMount() {
    const {
      params: { id }
    } = this.$router
    const { play, playlist } = this.props
    if (!play || !play.data) {
      playlist({ id })
    }
  }
render() {
 return(<View>1111</View>)
}
}

取数据

const { play: { data = {} } } = this.props

不能通过props获取到id只能像下面这样写了,比较麻烦,特别是接口多的时候

@connect(
  (state, props) => ({
    play: state.player // player redux的名字
  }),
  dispatch => ({
    playlist: bindActionCreators(playlist, dispatch)
  })
)
class PlayList extends Component {
 componentDidMount() {
    const {
      params: { id }
    } = this.$router
    const { play, playlist } = this.props
    if (!play[id] || !play[id].data) {
      playlist({ id })
    }
  }
render() {
 return(<View>1111</View>)
}
}

方法 getPlayerList

export const getPlayerList = (state, id) => {
  return state.player[id] ? state.player[id] : {}
}

然后取数据要通过 this.$router.params 获取 id

const { play } = this.props
const data = (play[id] || {}).data || {}

期望行为
[希望props可以获取路由上参数]

报错信息

[无]

系统信息

  • 操作系统: [e.g. mac os]
  • Taro 版本 [e.g. 👽 Taro v1.2.22]
  • Node.js 版本 [e.g. v8.11.4]
  • 报错平台 [weapp]
@taro-bot
Copy link

taro-bot bot commented Mar 28, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@dxhuii
Copy link
Author

dxhuii commented Apr 12, 2019

@luckyadam 这个还打算完善吗?等了两周了

@dxhuii
Copy link
Author

dxhuii commented May 22, 2019

已经找到其他方法解决这个问题了。

父组件

import Taro, { Component } from '@tarojs/taro'
import Player from '@/components/Player'

export default class Play extends Component {
  render() {
    const {
      params: { id, pid }
    } = this.$router
    return <Player vodid={id} pid={pid} />
  }
}

子组件

import Taro, { Component } from '@tarojs/taro'
import { bindActionCreators } from 'redux'
import { connect } from '@tarojs/redux'

import { playerLoad } from '@/store/actions/player'
import { getPlayerList } from '@/store/reducers/player'
import { getUserInfo } from '@/store/reducers/user'

@connect(
  (state, props) => ({
    userinfo: getUserInfo(state),
    player: getPlayerList(state, props.vodid, props.pid)
  }),
  dispatch => ({
    playerLoad: bindActionCreators(playerLoad, dispatch)
  })
)
class Play extends Component {
render() {
    return <View>demo</View>
}
}

这样就可以获取props上的值,以前的写法也不用改了

可以结帖了。

@dxhuii dxhuii closed this as completed May 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants