我试图建立一个论坛。
在客户端我有这个:
void initRouter() {
var router = new Router();
router.root
..addRoute(name: 'register', path: '/register', enter: (e) => showRegister(e, router), leave: leaveRegister)
..addRoute(name: 'login', path: '/login', enter: (e) => showLogin(e, router), leave: leaveLogin)
..addRoute(name: 'forum_general', path: '/forum', enter: (e) => showForum(e, router))
..addRoute(
name: 'forum_target',
path: '/forum/:nForum',
mount: (router) =>
router
..addRoute(
name: 'subforum_general',
path: '/subforum',
enter: (e) => showSubForum(e, router))
..addRoute(
name: 'subforum_target',
path: '/subforum/:subForum',
mount: (router) =>
router
..addRoute(
name: 'subject_general',
path: '/subject',
enter: (e) => showSubForum(e, router))
..addRoute(
name: 'subject_target',
path: '/subject/:nSubject',
enter: (e) => showSubForum(e, router))))
..addRoute(name: 'logout', path: '/logout', enter: (e) => showLogout(e, router))
..addRoute(defaultRoute: true, name: 'index', path: '/index', enter: showIndex);
router.listen();
}
当我走上一条简单的路径(如/forum
)时,它就可以了。但是当我添加一些信息(比如forum/test
)时,路由器就不会抓住这个网址......
即使我使用此..addRoute(name: 'subforum_general', path: '/forum/:nForum', enter: (e) => showSubForum(e, router))
,它也无法正常工作..
有什么想法吗?
答案 0 :(得分:1)
我无法在router
软件包上找到很多文档(假设您使用的是什么),并且其GitHub repo标记为"构建失败& #34; :/
另一个名为route
的路由包由Dart Authors维护,我已经使用了一点,并且似乎运行良好。如果你不能router
做你需要的事情,我肯定会考虑给它一个机会。
https://pub.dartlang.org/packages/route
main() {
var router = new Router()
..addHandler(new UrlPattern(r'/article/(\d+)'), showArticle)
..listen();
}
void showArticle(String path) {
var articleId = articleUrl.parse(req.path)[0];
// show article page with loading indicator
// load article from server, then render article
}