Angular2: Pass Component Page from Service to othe

2019-09-13 11:57发布

问题:

If I pass a Component Page to another via a service, that other Component fails to instantiate with "undefined [?]".

How to achieve this ? (Context: so that I can centralize Component Navigation in one Service)

Update

This is our current workaround, ugly like most of Ionic Beta and Angular Beta if I may say so. This is our "PassThroughComponent, that gets called each time we want to navigate from component to component dynamically via the navService (we got some algorithms there calculating a users journey):

  ngOnInit() {
        this.nextPage(this.navService.getNextPage())
    }

    nextPage(nextPageString) {
        switch (nextPageString) {
            case "Fairytales":
                this.navController.push(Fairytales);
                break;
            case "Bitcoin":
                this.navController.push(Bitcoin);
                break;
            case "Unicorns":
                this.navController.push(Unicorns);
                break;
            case "Rainbows":
                this.navController.push(Rainbows);
                break;
            case "Marbles":
                this.navController.push(Marbles);
                break;
            case "Dogs":
                this.navController.push(Dogs);
                break;
            case "Cats":
                this.navController.push(Cats)
                break;
            default:
                console.log("Unknown page", nextPageString)
        }
    }

Not Working:

import { Nav } from 'ionic-angular'

@Injectable()
export class NavService {

   @ViewChild(Nav) nav;
   public dismiss() {
       this.nav.setRoot(HomePage)
   }
   ...

browser_adapter.js:84 ORIGINAL EXCEPTION: TypeError: Cannot read property 'setRoot' of undefined

Also not working with navController.