我如何使用我通过 riot.route() 获得的参数


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

当URL更改时,我想将参数获取为"类别"并在<category-page>中使用它。我在<category-page>中尝试了console.log(this.opts.category),但我得到的是不确定的。

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

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

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

所以你的路由器代码应该看起来像这样:

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

最新更新