0

我试图在 ionic 中处理 web 视图的后退按钮 

为此,我使用 navcontoller 在 ionic 应用程序中导航

我收到一个错误,我在下面提到了使用 navcontrol

 MyApp_Host.ngfactory.js? [sm]:1 ERROR Error: StaticInjectorError(AppModule)[Nav -> NavController]: 
   StaticInjectorError(Platform: core)[Nav -> NavController]:
     NullInjectorError: No provider for NavController!
     at _NullInjector.get (core.js:1003)
     at resolveToken (core.js:1301)
     at tryResolveToken (core.js:1243)
     at StaticInjector.get (core.js:1111)
     at resolveToken (core.js:1301)
     at tryResolveToken (core.js:1243)
     at StaticInjector.get (core.js:1111)
     at resolveNgModuleDep (core.js:10896)
     at NgModuleRef_.get (core.js:12129)
     at resolveDep (core.js:12619)

我正在将我添加到我的网站的 ts 代码放入我收到错误消息。我在 navcontoller 的构造函数中定义了 navCtrl 并将其用于window.onpopstate

 window.onpopstate = function(event) {
    //alert('onpop');
    console.log(event);
    console.log('location: ' + document.location + ', state: ' + JSON.stringify(event.state));
    let str = event.target;
    console.log('Hash-----' + document.location.hash)
    let location:Array<String> = document.location.hash.split('/');
    console.log(window.location);
    //let url = document.location.toString();
    //console.log(url);
    //window.location.replace(url);
    console.log(location);
    console.log(location[1]);
    if (location[1] == "createTeam") {
        storage.get('team').then(val => {
            console.log("players-----------------------------")
            console.log(val);
            //console.log(val.length);
            if (val!= null) {
                let alert = alertCtrl.create({
                    title: "ALERT",
                    message: "Do you want to delete this team",
                    buttons: [
                        {
                            text: "Cancel",
                            role: "cancel",
                            handler: () => {
                                console.log("Cancel clicked");
                            }
                        },
                        {
                            text: "Ok",
                            handler: () => {
                                console.log("Ok clicked");
                                storage.remove("team");
                                navCtrl.pop();
                            }
                        }   
                    ]
                });
                alert.present();
            }
            else{
                navCtrl.pop();
            }
        });
    }
    else if(location[1] == "error"){
        alert('back error page');
        navCtrl.pop().then(()=>{
            navCtrl.push('MatchcenterPage');
        })
    } else {
        navCtrl.pop();
    }
    console.log(str);
};
4

1 回答 1

1

您可以订阅导航:

this._app.viewDidEnter.subscribe(x => {
    // your magic here
});

我认为的最佳实践:

  1. 创建一个导航提供程序,因为它将是一个单例并存储状态,不会导致您出现内存泄漏。
  2. 订阅导航并施展你的魔法。
于 2018-12-03T14:24:39.710 回答