配置 Web 应用的 URL 策略
Flutter Web 应用支持两种基于 URL 的路由的配置方式:
Hash(默认)
路径使用 # + 锚点标识符 读写,例如:flutterexample.dev/#/path/to/screen
。
Path
路径使用非 # 读写,例如:flutterexample.dev/path/to/screen
。
配置 URL 策略
让 Flutter 使用 path 策略,请使用 flutter_web_plugins
库中提供的 setUrlStrategy
方法。
import 'package:flutter_web_plugins/url_strategy.dart';
void main() {
usePathUrlStrategy();
runApp(ExampleApp());
}
Configuring your web server
PathUrlStrategy uses the History API, which requires additional configuration for web servers.
To configure your web server to support PathUrlStrategy, check your web server’s
documentation to rewrite requests to index.html
.Check your web server’s
documentation for details on how to configure single-page apps.
If you are using Firebase Hosting, choose the “Configure as a single-page app” option when initializing your project. For more information see Firebase’s Configure rewrites documentation.
The local dev server created by running flutter run -d chrome
is configured to
handle any path gracefully and fallback to your app’s index.html
file.
将 Flutter 应用部署在子目录下
更新 web/index.html
中的 <base href="/">
标签为你的应用部署路径。例如:如果你期望将 Flutter 应用部署在 myapp.dev/flutter_app
,则更改此标签为 <base href="/flutter_app/">
。