【问题标题】:How can I use parameter I get through riot.route()?如何使用通过 riot.route() 获得的参数?
【发布时间】:2017-03-21 19:53:50
【问题描述】:
riot.route('/*', function(category) {
    riot.mount('#main', 'category-page', category)
})

当 URL 更改时,我想将参数作为“类别”并在 <category-page> 中使用它。 我试过console.log(this.opts.category)in <category-page>,但我得到的是未定义的。

riot.route('/*', function(category) {
    riot.mount('#main', 'category-page', category)
    console.log(category)
})

当我像上面那样编码时,console.log(category) 运行良好。所以我认为传递或获取参数是错误的。我尝试了很多案例,但我无法解决它。 请帮我解决这个问题。

【问题讨论】:

    标签: javascript riot.js riot


    【解决方案1】:

    调用时根据 riot.js router api 文档 riot.mount(selector, tagName, [opts]) 你应该在你的标签中传递一个将被设置为this.opts 的对象。

    所以你的路由器代码应该是这样的:

    riot.route('/*', function(category) {
        riot.mount('#main', 'category-page', {category: category})
    });
    

    【讨论】:

    • 感谢您的帮助。它运作良好。我检查了文档,我认为上面写着riot.mount(selector, tagName, api)。无论如何,我真的很感谢你!
    猜你喜欢
    • 2021-12-13
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2021-07-19
    相关资源
    最近更新 更多