gpt4 book ai didi

dart - 如何使用Dart Route API

转载 作者:行者123 更新时间:2023-12-03 03:28:46 25 4
gpt4 key购买 nike

我创建了一个示例应用程序来测试Dart Route API。我有以下代码:

urls.dart

library urls;

import 'package:route/url_pattern.dart';

final homeUrl = new UrlPattern(r'/');
final otherScreenUrl = new UrlPattern(r'/other_screen/');

main.html

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<div id="sample_container_id">
<p id="sample_text_id"></p>
</div>

<script type="application/dart" src="main.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>

main.dart

import 'dart:html';
import 'package:route/client.dart';

import 'urls.dart';

void main() {
var router = new Router()
..addHandler(homeUrl, _showHome)
..addHandler(otherScreenUrl, _showOtherScreen)
..listen();

querySelector("#sample_text_id")
..text = "Click me!"
..onClick.listen(_gotoOtherScreen);
}

_gotoOtherScreen(MouseEvent event) {
// I am trying to navigate to the "other screen" by using history.pushState here
window.history.pushState({'url' : otherScreenUrl}, "other screen", otherScreenUrl);
}

_showHome(String path) {
querySelector("#other_element")
..remove();
}

_showOtherScreen(String path) {
querySelector("#sample_container_id")
..append(new SpanElement()
..innerHtml = "now in other screen"
..id = "other_element");
}

运行应用程序,然后单击 <p>标记时出现以下错误:

Breaking on exception: Illegal argument(s): No handler found for /test/web/main.html

Exception: Illegal argument(s): No handler found for /test/web/main.html Router._getUrl (package:route/client.dart:53:7) Router.handle (package:route/client.dart:71:22)
Router.listen. (package:route/client.dart:102:15)

Breaking on exception: type 'UrlPattern' is not a subtype of type 'String' of 'url'.

Exception: type 'UrlPattern' is not a subtype of type 'String' of 'url'. _gotoOtherScreen (http://127.0.0.1:3030/test/web/main.dart:18:27)



应该如何使用Route API?我究竟做错了什么?

最佳答案

以下是解决上述问题的更新代码:

urls.dart

library urls;

import 'package:route/url_pattern.dart';

final homeUrl = new UrlPattern(r'(.*)/');
final homeUrlWithFile = new UrlPattern(r'(.*)/main.html');
final otherScreenUrl = new UrlPattern(r'(.*)/other_screen');

main.html

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<div id="sample_container_id">
<a href="/other_screen">click me!!</a>
</div>

<script type="application/dart" src="main.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>

main.dart

import 'dart:html';
import 'package:route/client.dart';

import 'urls.dart';


void main() {
var router = new Router()
..addHandler(homeUrl, _showHome)
..addHandler(homeUrlWithFile, _showHome)
..addHandler(otherScreenUrl, _showOtherScreen)
..listen();
}

_showHome(String path) {
var e = querySelector("#other_element");
if (e != null) e.remove();
}

_showOtherScreen(String path) {
querySelector("#sample_container_id")
..append(new SpanElement()
..innerHtml = "now in other screen"
..id = "other_element");
}

关于dart - 如何使用Dart Route API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20593058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com