在 Taro 的页面和组件类中,this 指向的是 Taro 页面或组件的实例,例如

    1. import Taro, { Component } from '@tarojs/taro'
    2. import { View } from '@tarojs/components'
    3. export default class Menu extends Component {
    4. static defaultProps = {
    5. data: []
    6. }
    7. constructor(props) {
    8. super(props)
    9. this.state = {
    10. checked: props.checked
    11. }
    12. }
    13. componentWillMount () {
    14. console.log(this) // this -> 组件 Menu 的实例
    15. }
    16. render () {
    17. return <View />
    18. }
    19. }

    但是一般我们需要获取 Taro 的页面和组件所对应的小程序原生页面和组件的实例,这个时候我们可以通过 this.$scope 就能访问到它们。

    所以当调用一些 API 需要传入小程序的页面或者组件实例时,可以直接传入 this.$scope,例如 Taro.createCanvasContext(canvasId, this) 这个 API,第二个参数就是自定义组件实例 this,在 Taro 中就可以如下使用

    1. Taro.createCanvasContext(canvasId, this.$scope)