这是我的main.dart:
import 'dart:html';
import 'package:route/client.dart';
import 'urls.dart';
void main() {
var router = new Router()
..addHandler(homeUrl, showHome)
..addHandler(galleryurl, showGallery)
..listen();
HttpRequest.getString("home.html").then((html){
querySelector("#container").innerHtml = html;
});
}
void showHome(String path){
HttpRequest.getString("home.html").then((html){
querySelector("#container").innerHtml = html;
});
}
void showGallery(String path){
HttpRequest.getString("gallery.html").then((html){
querySelector("#container").innerHtml = html;
});
}
urls.dart:
library urls;
import 'package:route/url_pattern.dart';
final homeUrl = new UrlPattern(r'(.*)/');
final homeUrlWithFile = new UrlPattern(r'(.*)/index.html');
final contacturl = new UrlPattern(r'(.*)/contact');
final galleryurl = new UrlPattern(r'(.*)/gallery');
final allUrls = [homeUrl, contacturl, galleryurl];
和我的身体在index.html:
<body>
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="/">Home</a></li>
<li><a href="/gallery">Gallery</a></li>
</ul>
</div>
</div>
</nav>
<main id="container">
</main>
<footer class="footer" >
<div class="col-md-10 col-md-offset-1 col-sm-12">here's footer</div>
</footer>
<script src="packages\jquery_for_pub\1.11.0\jquery-1.11.0.min.js"></script>
<script src="packages/bootstrap/js/bootstrap.js"></script>
<script type="application/dart" src="main.dart"></script>
<script data-pub-inline src="packages/browser/dart.js"></script>
</body>
我的问题是当我去localhost:8080 / gallery时,我收到错误404.但如果我打开localhost:8080并点击图库链接,它就可以了。
我想将它重定向到index.html,并且#container的内容在localhost:8080 / gallery中打开时更改为gallery。 谢谢你的帮助
答案 0 :(得分:0)
如果使用pushState(默认),则需要服务器支持才能将请求重定向到正确的URL。例如,这通常委托给nginx 或者,您可以禁用pushState并使用片段
var router = new Router(useFragment: true)
..addHandler(new UrlPattern(r'(.*)#something'), (_) => view = 'home')