flutter web 为什么网址中有主题标签?

lor*_*has 1 flutter flutter-web

我想知道为什么 URL 中有主题标签?对于 Flutter Web 应用程序,例如:http://localhost:64392/#/home即使在生产中也是相同的结果。

我想知道为什么以及是否可以删除它?

Men*_*elG 8

要删除#URL 中的 ,请参阅此处

要将 Flutter 配置为使用路径,请使用 SDK 中 flutter_web_plugins 库提供的 usePathUrlStrategy 函数:

import 'package:flutter_web_plugins/url_strategy.dart';

void main() {
  usePathUrlStrategy();
  runApp(ExampleApp());
}
Run Code Online (Sandbox Code Playgroud)

并将其添加到您的pubsepc.yaml

import 'package:flutter_web_plugins/url_strategy.dart';

void main() {
  usePathUrlStrategy();
  runApp(ExampleApp());
}
Run Code Online (Sandbox Code Playgroud)

此外,您还可以使用该go_router包。

请参阅关闭哈希

void main() {
  // turn on the # in the URLs on the web (default)
  // GoRouter.setUrlPathStrategy(UrlPathStrategy.hash);

  // turn off the # in the URLs on the web
  GoRouter.setUrlPathStrategy(UrlPathStrategy.path);

  runApp(App());
}
Run Code Online (Sandbox Code Playgroud)

  • 它接缝的“#”字符告诉浏览器这是一条客户端路由,导航应该由应用程序处理,而不是向服务器发出新的请求。Flutter 默认使用主题标签约定为客户端路由提供一致且可预测的 URL 结构。 (3认同)