Flutter Web 应用程序的 URL 中删除前导`#`

接上文

作者:坚果

公众号:“大前端之旅

华为云享专家,InfoQ签约作者,阿里云专家博主,51CTO博客首席体验官,开源项目GVA成员之一,专注于大前端技术的分享,包括Flutter,小程序,安卓,VUE,JavaScript。

然后大家是不是发现上面有个# ,说实话,我看着也很烦,那么如何解决呢,其实也简单。导入下面的包

url_strategy: ^0.2.0

image-20220307083936412

如何安装呢?控制台运行

flutter pub add url_strategy

url_strategy

Flutter 应用程序包,允许使用单行代码设置 Web URL 策略。

import 'package:url_strategy/url_strategy.dart';

void main() {
  // Here we set the URL strategy for our web app.
  // It is safe to call this function when running on mobile or desktop as well.
  setPathUrlStrategy();
  runApp(MyApp());
}

现在,您的 Flutter Web 应用程序的 URL 中将不再有前导#

即使在 web 以外的任何其他平台上运行(这是这个包的重点),调用该函数也是安全的。这意味着您可以setPathUrlStrategy在移动设备或台式机上运行时安全地调用 - 这将只是一个 noop。

image-20220307084109551

所以大家看到了吗?没有了哦。

猜你喜欢

转载自blog.csdn.net/qq_39132095/article/details/123321815