角路由器导航问题

时间:2020-08-22 12:26:01

标签: angular angular-router

我在了解Angular Router时遇到问题。

这是我的问题: 我目前在此网址/路由中-

https://localhost:4200/#/db/accounts/resetpassword?userId=7&resetToken=xyz

当我使用路由器+ ActivatedRoute导航时

this._router.navigate(['login'], { relativeTo: this._actRoute })
this._router.navigate(['./login'], { relativeTo: this._actRoute })
this._router.navigate(['../login'], { relativeTo: this._actRoute })
this._router.navigate(['../../login'], { relativeTo: this._actRoute })

...我被发送到

https://localhost:4200/#/db/accounts/resetpassword/login

当我导航到

this._router.navigate(['../../../login'], { relativeTo: this._actRoute })

...我被发送到

https://localhost:4200/#/db/accounts/login

...我要发送到的地方。

我的问题是,最后一个如何替换URL的末段而前四个不替换。 我觉得我缺少路由工作的一些基本知识。

1 个答案:

答案 0 :(得分:1)

对于感兴趣的任何人,这就是我现在要解决的方法。我真的不相信

class _TestState extends State<Test> {
  Future<String> current;

  @override
  void initState() {
    super.initState();
    current = getCurrentWeight();//Obtain your future
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: current,//Pass the future
      builder: (context, snapshot) {
        if(snapshot.hasData) {//Show data only when it's available
          return Container(
            child: Center(
              child: Text(snapshot.data)//Obtain data here
            ),
          );
        }
        return CircularProgressIndicator();//Show this otherwise
      }
    );
  }

  Future<String> getCurrentWeight() async {
    List<HistoryData> historyList = historyDataFromJson(await rootBundle.loadString('json_files/history.json'));
    var history = historyList[historyList.length-1];
    String current = history.weight;
    return current;
  }
}

因为从不同的模块可以正常工作

this._router.navigate(['../../../login'], { relativeTo: this._actRoute })

如果有人知道更好的方法,请发表评论。 另外,这可能需要对碎片等进行一些额外的检查。

this._router.navigate(['../../login'], { relativeTo: this._actRoute })