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

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.

html:
<ion-buttons>
<button ion-button (click)="goAnOtherPage()">Go an Other Page </button>
</ion-buttons>
OR
<ion-label [navPush]="anOtherPage">Go an Other Page</ion-label>
in TS:
import { NavController } from 'ionic-angular';
import { AnOtherPage } from '/anOtherPage';
anOtherPage: AnOtherPage;
constructor(public navCtrl: NavController) {}
goAnOtherPage() {
this.navCtrl.setRoot(anOtherPage);
}

html
<ion-button (click)="nextpage()">Home</ion-button>
ts
import { Router } from '#angular/router';
constructor(private route: Router) { }
nextpage() {
this.route.navigate(['/home']);
}

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
<button (click)="navigateToOtherPage()">Next Page<button>
or you can use an a, span or div in a similar way
<a (click)="navigateToOtherPage()">Other Page</a>
<span (click)="navigateToOtherPage()">Other Page</span>
<div (click)="navigateToOtherPage()">Other Page</div>
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.

you can also use angular to do this for you:
<button ng-click="doSomething(withSomebody)"> Submit
</button>
You would need to have a controller to and define doSomething()
$scope.doSomething=function(somebody){
...insert code here
}
If you needed to change your state, the inject $state into your controller and use $state.go(stateName)
ref: https://github.com/angular-ui/ui-router/wiki

Please use below code in Ionic 4 to go one page to another page:
<ion-button expand="block" routerLink="/dashboard" routerDirection="root">Home</ion-button>
(routerLink) to set the page name to when you go to the page.

For Ionic 5 - below code is working for me, with Ionic 5 + Angular 9 combination,
Step 1 - In html file, add the code to "define" the function,
<ion-button (click)="gotoHomePage()">Go to Homepage</ion-button>
Step 2 - In the constructor of your class, you can Inject the NavController - Injecting NavController will always get you an instance of the nearest NavController, regardless of whether it is a Tab or a Nav.
constructor(public navCtrl: NavController) { }
Step 3 - In TS file for your component, add the method that you are calling on click of ion-button,
gotoHomePage() {
this.navCtrl.navigateForward('home');
}
Here the 'home' is defined route in your app-routing.module.ts file, like below,
{ path: 'home', loadChildren: () => import('./home/home.module').then(m => m.HomePageModule) },

You can use this simple way:
<ion-button routerLink="/myPageName" routerDirection="root">
Go to myPageName!
</ion-button>

Many solutions I have tried don't work, but this one below it does for me.
To go from home page to another page just watch this video:
https://www.youtube.com/watch?v=paRZyKDHPak
in the home.page.ts file add
import { Router } from "#angular/router";
set the constructor
constructor(public router:Router) {}
create the function
redirectToOther(){
this.router.navigateByUrl('other');
}
you guys can add a "go back button" to go back to the home page just adding this HMTL code in other.page.html, that's it
<ion-buttons slot="start"><ion-back-button defaultHref="home"></ion-back-button></ion-buttons>

You can call a route like this.
<button onclick="location.href='/pageToRoute/param'" block>
</button>

Basically you would navigate to a different page in the same way as if you were in Angular.
Important thing to remember is that Ionic routes are essentially Angular routes.
To learn more about how they work, visit: https://angular.io/guide/router
To answer your question.
Let's imagine you want to redirect a user to your About page.
Within your app-routing.module.ts you have a path that goes:
{
path: 'about',
loadChildren: () => import('./pages/about/about.module').then( m => m.AboutPageModule)
}
Then in the tags of a button, you want add the classical Angular router call that goes like this:
<ion-button color="dark" expand="block" fill="outline" routerLink="/about">Go!</ion-button>
Basically, routerLink="/name-of-the-path" can be used on your buttons or tags to redirect the user to the desired location.
Hope this helps!

This is what i used
<ion-nav-buttons side="right" >
<a href="templates/yourpage.html">
<button class="button">
Show your page
</button>
</a>
</ion-nav-buttons>

Place button inside anchor tag.
<button>Name</button>
You can also refer this link: http://www.w3schools.com/tags/tag_a.asp

Related

Create Route to Simple Page Reaction Commerce

I am trying to setup a simple static page for about us, based on this tutorial (https://docs.reactioncommerce.com/reaction-docs/master/plugin-routes-6). The problem is that there is no real explanation on what I need to do outside adding an entry to the registry.js file. While they do have the plugin example that I could copy, I would like to know what I need to just add a simple static page to Reaction Commerce. Thanks.
Wade
To create the simple route for the page, given tutorial is what we all got.
To Create route for a page:
I will break it down for you in following steps:
I will assume that you know we have to add our code in the /imports/plugin/custom directory only. You can override all the core functionality from here.
Let's get started:
You need to add route details in the registry under register.js file.
registry:[
{
route:"/about",
name:"about",
template:"aboutUs",
workflow:"coreWorkflow"
}
],
Create the component for a new page as
/imports/plugin/custom/YOUR_PLUGIN/client/components/about.js in your plugin.
import React, { Component } from "react";
import { registerComponent } from "/imports/plugins/core/components/lib";
import { Meteor } from "meteor/meteor";
import { Col } from 'reactstrap';
class About extends Component {
render() {
return (
<div className="container-main">
About Us Page
</div>
);
}
}
registerComponent("about", About);
Add the button to route to the new page, in any component, from where you can give link to About page.
<Components.Button
label="About"
onClick={handleClick}
/>
Add function to handle the click.
handleClick() {
return Reaction.Router.go("/about");
}
Hope this solves your query!
PS: I know this code can be shortened, I have written it in this way so that beginners can understand it faster. Please don't hesitate to correct the answer if I am wrong. :)

METEOR: how to redirect after logout

I am quite new to Meteor & React. Here I would like to redirect my currect user to home page whenever the logout button is pressed. Attached you can see the protected page template with the logout button.
Please note that I am working with the latest versions (Meteor 1.6.1 and React V4).
import React from 'react';
import { Accounts } from 'meteor/accounts-base';
export default class Link extends React.Component{
onLogout(){
Accounts.logout()
};
render(){
return(
<div>
<p>Private Content goes here</p>
<button onClick={this.onLogout.bind(this)}>Logout</button>
</div>
);
}
};
any kind of support will be appreciated.
There are two main options to use here:
1. Pass a callback to Accounts.logout(func)
This is the simplest but mixes the return behavior into your component, which is not ideal.
2. Use Accounts.onLogout(func)
You could put this with your accounts initialization or with your router code, whichever keeps the logic grouped together best for your app.
In that callback, you'll want to use your router to redirect. The exact syntax will depend on your router, but will generally look like:
Router.go('/')
Another way, if you are setting things via meteor-useraccounts way...
const myLogoutFunc = function() {
FlowRouter.go('/login');
}
AccountsTemplates.configure({
// Hooks
onLogoutHook: myLogoutFunc,
onSubmitHook: mySubmitFunc,
preSignUpHook: myPreSubmitFunc,
postSignUpHook: myPostSubmitFunc,
});
Template event code is something like this..
'click .logout': () => {
AccountsTemplates.logout();
}
Read in detail here https://github.com/meteor-useraccounts/core/blob/master/Guide.md

How to navigate from one page to another in angular2

I am new to angular2. How to navigate from one page to another in angular2?. I am using router-outlets for that. It just redirects between one component to another but i want to redirect from home page to another when i click on dropdown items.Do i need to use page-router-outlets for that? If so how to use that and how to install nativescript and all. Thanks in advance for the reply.
I am using app.route.ts
import { Routes } from '#angular/router';
import { HomeComponent } from './home.component';
import { AboutComponent } from './about.component';
export const routes: Routes = [
{ path: 'home' , component: HomeComponent,pathMatch: 'full' },
{ path: 'about', component: AboutComponent }
];
In app.component.html i have
<ul>
<li id="home"><a [routerLink]="['/home']" >Home</a> </li>
<li id="about"><a [routerLink]="['/about']" >About</a></li>
</ul>
<router-outlet></router-outlet>
In app.module.ts I have configured the router.
When i click on home hyperlink it will route to app.component.html page only. Its not redirecting to another page and app.component.html is always shown on the top of the page every single time I go to a new page.
How to route to new page without showing the app.component.html in the new page.

Rendering React Components to Specific Div Using Routing

I am using FlowRouter as a router for a Meteor/React application I am trying to create. I'm having a very hard time trying to get my react components to render in specific places. Does anyone know how to do this?
So on my landing page, when I click a button, I want to route to a secondary page. I have three different components that I want to render in certain parts of the page. I've been using ReactLayout.render(), but I can't seem to make sure components get rendered in certain areas. I thought document.getElementById would work
ReactLayout.render(LandingPage, document.getElementById("landing-page")
but it hasn't been.
The second parameter of ReactLayout.render expects an object. If you want to render several components into your LandingPage element, it might look something like this:
LandingPage = React.createClass({
render() {
return (
<div className="app-root">
<AppHeader />
<div className="container">
{this.props.testOne}
</div>
<div className="app-root">
{this.props.testTwo}
</div>
</div>
);
}
});
Then render using:
FlowRouter.route( '/testRedirect', {
name: 'test',
action() {
ReactLayout.render( Default, { testOne: <TestOneComponent />, testTwo: <TestTwoComponent /> } );
}
});

Styling of ion-side-menu-content

I want to add css styles to my login page. The application works as:
ion-side-menus where is:
ion-side-menu-content inside of it is:
ion-nav-view name="content"
Controller looks like:
.state('myapp.login', {
url : "/login",
views: {
'content': {
templateUrl: "myApp-core.login.html",
controller : 'LoginController'
}
}
})
The problem is that when I changed a main css it causes on all sub pages but I want to change only my login page (myApp-core.login.html). Thanks for any help.
Simplest make your own css class "xxx-login" and apply it on html tag?
if you want to extend ionic class, you can use Less, ionic provide all tools to build it.

Resources