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

百度小程序,嵌套map,第二层s-for绑定有误 #1223

Closed
HenryLulu opened this issue Nov 23, 2018 · 7 comments
Closed

百度小程序,嵌套map,第二层s-for绑定有误 #1223

HenryLulu opened this issue Nov 23, 2018 · 7 comments
Assignees

Comments

@HenryLulu
Copy link

image
编译成swan后,第二层s-for="$anonymousCallee__15"
image
但state中,$anonymousCallee__15是在第一层下的。
image
所以s-for的正确引用应该是s-for="key.$anonymousCallee__15"

@taro-bot
Copy link

taro-bot bot commented Nov 23, 2018

欢迎提交 Issue~

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

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

Good luck and happy coding~

@HenryLulu HenryLulu changed the title 百度小程序,嵌套map,第二层无效 百度小程序,嵌套map,第二层s-for绑定有误 Nov 23, 2018
@yuche
Copy link
Contributor

yuche commented Nov 23, 2018

你这个在微信小程序能跑吗,顺便 JSX 源码贴一份字符,不要截图,整个 render 函数一起贴出来。顺便带上你的版本号

@taro-bot
Copy link

taro-bot bot commented Nov 23, 2018

CC @yuche

@HenryLulu
Copy link
Author

感谢回复~

微信小程序也跑不起来
taro版本:1.1.9
render函数如下:

 render() {
    const ListCards = Object.keys(this.props.lesson).map(key => {
        const curKey = this.props.lesson[key];
        const ListItems = Object.keys(curKey.sub).map(subkey => {
            return (<Text className="listitem">{subkey}</Text>)
            // return subkey
        });
        return (
            <View className="listcard">
                <Text className="listtitle">{curKey.name}</Text>
                <View className="sublist">
                    {ListItems}
                </View>
            </View>
        );
    });

    return (
        <View className="lesson">
            <View className="sidebar">
                {Object.keys(this.props.lesson).map(key => (
                    <Text className={`sideitem ${this.state.currentUnit === key ? 'current' : ''}`}
                        key={key}>{this.props.lesson[key].unit}</Text>
                ))}
            </View>
            <ScrollView className="list" scrollY={true}>
                {ListCards}
            </ScrollView>
        </View>
     );
  }

this.props.lesson 数据格式如下:

{
  "u0": {
    "unit": "第一章",
    "name": "第一章 集合与函数概念",
    "sub": {
      "l0": "集合",
      "l1": "函数及其表示",
      "l2": "函数的基本性质"
    }
  },
  "u1": {
    "unit": "第二章",
    "name": "第二章 基本初等函数(I)",
    "sub": {
      "l0": "指数函数",
      "l1": "对数函数",
      "l2": "幂函数"
    }
  },
  "u2": {
    "unit": "第三章",
    "name": "第三章 函数的应用",
    "sub": {
      "l0": "函数与方程",
      "l1": "函数模型及其应用"
    }
  }
}

@yuche
Copy link
Contributor

yuche commented Nov 26, 2018

下个版本会修复,临时的解决方案是把循环内的 JSX 定义挪一下:

const ListCards = Object.keys(this.props.lesson).map(key => {
    const curKey = this.props.lesson[key];
    return (
        <View className="listcard">
            <Text className="listtitle">{curKey.name}</Text>
            <View className="sublist">
                {Object.keys(curKey.sub).map(subkey => {
                    return (<Text className="listitem">{subkey}</Text>)
                    // return subkey
                });}
            </View>
        </View>
    );
});

@HenryLulu
Copy link
Author

这种写法会报sub undefined,第二层作用域可能不太对。
我这边可行的方案是把sublist单抽了个component出来。

@yuche
Copy link
Contributor

yuche commented Nov 26, 2018

那你现在就这样:

const ListCards = Object.keys(this.props.lesson).map(key => {
    const curKey = this.props.lesson[key];
    const subs = Object.keys(curKey.sub);
    return (
        <View className="listcard">
            <Text className="listtitle">{curKey.name}</Text>
            <View className="sublist">
                {subs.map(subkey => {
                    return (<Text className="listitem">{subkey}</Text>)
                    // return subkey
                });}
            </View>
        </View>
    );
});

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