我正在使用riotjs库,并有两个标签“评论列表”和“评论”。
评论列表:
<comment-list> <div each={com in comments} > <comment level={opts.level} comment={com} type={opts.level == 0 ? "parent" : "child"} ctype={commentType}></comment> </div> <div show={opts.level == 0}> <a id="load-trigger" onclick={fetchMore}>Moar comments</a> </div> var self = this; self.commentType = (self.opts.level === 0 ? "parent" : "child"); </comment-list>和评论:
<comment> ... some html ... <div class="row" if={opts.level == 0}> <div class="col s11 offset-s1 m11 offset-m1 l11 offset-l1" style="padding-left: 0;"> <comment-list level={1} fetchMore={fetchMore}></comment-list> </div> </div> </comment>comment-list.tag初始化为level = 0.它应该将此级别值传递给comment.tag 。 问题是当我尝试访问comment.tag中的 opts.level时,我得到'undefined'。 同样适用于ctype 。 类型始终是“子”,就好像comment-list.tag中的opts.level没有被定义一样。
Ps:我不认为这可能是因为注释列表中的0被认为是null或'undefined',因为我对值1具有相同的行为,例如我是否有些错误直接从opts对象传递值?
I am using riotjs library and have two tags "comment-list" and "comment".
comment-list :
<comment-list> <div each={com in comments} > <comment level={opts.level} comment={com} type={opts.level == 0 ? "parent" : "child"} ctype={commentType}></comment> </div> <div show={opts.level == 0}> <a id="load-trigger" onclick={fetchMore}>Moar comments</a> </div> var self = this; self.commentType = (self.opts.level === 0 ? "parent" : "child"); </comment-list>and comment :
<comment> ... some html ... <div class="row" if={opts.level == 0}> <div class="col s11 offset-s1 m11 offset-m1 l11 offset-l1" style="padding-left: 0;"> <comment-list level={1} fetchMore={fetchMore}></comment-list> </div> </div> </comment>comment-list.tag is initialized with level = 0. It is supposed to pass this level value to comment.tag. Problem is when I try to access opts.level in comment.tag, I get 'undefined'. Same goes for ctype. type is always "child" as if opts.level in comment-list.tag wasn't defined.
Ps: I don't think it might be due to 0 in comment-list being considered as null or 'undefined' as I have the same behavior with value 1 for instance Do I do someting wrong passing a value straight from the opts object ?
最满意答案
每个创建一个子范围,所以你需要看父母。
将{opts.level}更改为{parent.opts.level}
所以你的<comment-list>是你当前的范围,你的each={com in comments}创建一个子范围。 您的opts.level属于当前/父级,而不属于已迭代的单个范围。
https://github.com/riot/riot/issues/1720
这是我今年早些时候针对这个问题提出的github问题。
The each creates a child scope so you need to look at the parent.
Change {opts.level} to {parent.opts.level}
So your <comment-list> is your current scope, and your each={com in comments} creates a child scope. Your opts.level belongs to the current/parent, not to the individual scope that's been iterated.
https://github.com/riot/riot/issues/1720
Here's the github issue i raised earlier this year for this exact question.
更多推荐
发布评论