how to go to another page with a button click with ionic?

后端 未结 12 1898
粉色の甜心
粉色の甜心 2020-12-31 10:26

I have been trying to work with the code, I added the button but I don\'t know how to link it to another page when clicking on it.

12条回答
  •  鱼传尺愫
    2020-12-31 10:45

    Navigation works differently in ionic and it is not advised to simply use an anchor tag and the path to the file.

    Here is an example which works. In the relevant TS file import the page(s) you want to navigate to like so

    import { OtherPage} from "../pagefolder/pagecontroler";
    import { YetAnotherPage} from "../pagefolder/pagecontroler";
    

    Ensure that you have the navigation controller imported and injected into the constructor of your current TS file. It should look like the following in case you are not sure.

    import { NavController } from "ionic-angular";
    

    Then in constructor

    constructor(public navCtrl: NavController, .....) {
    
    }
    

    Once you have the navigation controller and the page(s) imported you can proceed to create a function like so

    navigateToOtherPage(): void {
       this.navCtrl.push(OtherPage);
    }
    

    That completes the navigation setup for your TS file. Now you go to the relevant template you want to navigate from and you can use any tags you want for instance

    or you can use an a, span or div in a similar way

    Other Page
    Other Page
    
    Other Page

    You don't necessarily have to use the click event you can use whatever event you want. The main things are that you have the navigation controller imported and injected in your constructor as shown above and you have the page(s) you want to navigate to imported as well and you only use the navigation controller to push pages you want to navigate to.

提交回复
热议问题