I'm having the following issue. I created an ionic projekt and generated a Homepage and a Register page.
When I try to do the navigation from home to register, and when I click on the register button it brings me back to homepage, instead of going to register page. I don't know what I'm doing wrong, I read bassicaly everything I found, and it seems that I am doing everything correctly. I also tried with a href, but I still have the same problem.
Here is the button that i generated in my Homepage html:
<ion-button (click)="register()">Register</ion-button>
Here is the Funktion in ts-file:
register() {
this.navctrl.navigateForward(['register']);
}
And here is the auto generated path in the routing page:
{ path: 'register', loadChildren: './register/.module#RegisterPageModule' },
Here is the syntax :
import { Router } from '#angular/router';
...
constructor(private router: Router){}
register() {
this.router.navigate(['/register']);
}
Here you go.
Related
In my ionic project I have 3 pages: HomeScreen, SearchPage & ScannerPage.
I navigate to SearchPage & ScannerPage from my HomeScreen via a routerlink as in my examples below:
<ion-button routerLink="/manual-qr-search" color= "tertiary" expand="block">Check Out</ion-button>
<ion-button disabled={{this.myService.getTempCardDisabled()}} routerLink="/manual-temperature-scanner" color= "primary" expand="full" class="flex-child"><ion-icon name="keypad-outline"></ion-icon>MANUAL</ion-button>
and I navigate back to the HomeScreen from my SearchPage using the android back button as follows:
this.platform.backButton.subscribeWithPriority(99999, () => {
this.router.navigate(['home-screen']);
});
and I navigate back to the HomeScreen from my ScannerPage using the android back button as follows:
this.platform.backButton.subscribeWithPriority(99999, () => {
this.router.navigate(['home-screen']);
});
My problem is the HomeScreen's ngOnInit() hits when I navigate from SearchPage but it doesn't hit when I navigate from ScannerPage and I want to know why.
I do not call ngOnDestroy() at all on any page.
I can put in a check to see when I navigate from another page so that the code inside the HomeScreen's ngOnInit() doesn't hit when I don't want it to but I feel like that is bad programming.
you can do the following if you want to check for event on page navigation change.
import { ActivatedRoute } from '#angular/router';
public class yourClassName{
constructor( private route: ActivatedRoute){
this.route.queryParams.subscribe((params) => {
// Do things when page changes routes
})
}
}
I have noticed that I had 2 paths set to my HomeScreen in my app-routing.module.ts folder which caused it to act this way. I added another route to call on ionViewWillEnter() and forgot to comment out the previous route.
i am trying to create a QR code scanner app where I done by following ionic barcode plugin but when I click on the button i need to the camera view to be viewed inside my ion-item the camera should give a live feed of what it see.
<ion-content padding >
<ion-item style="background-color: transparent;">
<!--MY CAMERA VIEW INSIDE HERE SHOULD ALWAYS SHOW ME LIVE FEED -->
</ion-item>
<ion-item>
<!-- DATA FROM THE SCANNED QR CODE-->
</ion-item>
</ion-content>
constructor(public navCtrl: NavController,
public barcodeScanner: BarcodeScanner) {
}
ionViewWillEnter(){
this.open_bar();
}
open_bar(){
console.log("open camera clicked")
this.barcodeScanner.scan().then(barcodeData => {
console.log('Barcode data', barcodeData);
}).catch(err => {
console.log('Error', err);
});
}
}
when page loads 50% is for camera and 50% is for data from the QR code data the camera should always be live when page loads
I am expecting a view like this
You could somehow overwrite the barcode plugin's code to open camera with this other plugin instead of regular Cordova camera. This plugin lets you achieve this with little effort. What's complicated is to make that adaptation in barcode's plugin
You cannot do that with the BarcodeScanner plugin, instead, you should use the QRScanner module
Installation
ionic cordova plugin add cordova-plugin-qrscanner
npm install --save #ionic-native/qr-scanner
Add it to your providers in app.module.ts
#NgModule({
.....
providers: [
....
QRScanner
]
})
Use
In your .ts file, add QRscanner to your constructor
constructor(private qrScanner: QRScanner) {}
And call the QRScanner like this :
this.qrScanner.prepare()
.then((status: QRScannerStatus) => {
if (status.authorized) {
// You can scan your QR Code
this.scanSub = this.qrScanner.scan().subscribe((text: string) => {
console.log('Scanned value', text);
this.qrScanner.hide();
this.scanSub.unsubscribe();
});
this.qrScanner.show();
} else if (status.denied) {
console.log('Camera permission denied');
} else {
console.log('Permission denied for this runtime.');
}
})
.catch((e: any) => console.log('Error is', e));
}
QRScanner doesn't open a view to display camera (like BarcodeScanner does), but simply displays the view in background, so make sure to make the preview area transparent
I am using ReactNavigation library in my react-native project and since 6 hours I am trying to navigate from one screen to others screen and have tried every possible way but I think I am not able to get the logic properly.
This is my project structure.
Here
The way I am doing it.
const AppStack = StackNavigator({ Main: Feeds });
const AuthStack = StackNavigator({ Launch: LaunchScreen, });
export default SwitchNavigator({
Auth: AuthStack,
App: AppStack
});
In my LaunchScreen.js
const SimpleTabs = TabNavigator(
{
Login: {
screen: Login,
path: ""
},
SignUp: {
screen: SignUp,
path: "doctor"
}
},
);
<SimpleTabs screenProps={{rootNavigation : this.props.navigation }}/>
But the problem is in my LaunchScreen Component there is a TabNavigator which contains my other two components Login.js and SignUp.js but the button in my Login.js doesn't navigate it to Feed.js.
When you click on the button this is performed.
signInAsync = async () => {
await AsyncStorage.setItem('userToken', 'abc');
this.props.navigation.navigate('Main');
console.log("AAAAAsSSS");
};
My LaunchScreen.js contains a TabNavigation which lets you slide between two components ie. Login.js and SignUp.js.
Now when you click on the Login button which is in Login.js component it will authenticate the user and will switch the entire LauchScreen.js component with the Feed.js component.
I am a noob to react-native.
You can use react-native-router-flux (npm install --save react-native-router-flux)
just make one Navigator.js file and define each page you wanted to navigate.
import React from 'react';
import { Router, Scene } from 'react-native-router-flux';
import LaunchScreen from '../components/LaunchScreen.js';
import Feed from '../components/Feed.js';
const Navigator = () => {
return (
<Router>
<Scene key="root">
<Scene key="lauchscreen" component={LaunchScreen} hideNavBar initial />
<Scene key="feedscreen" type="reset" hideNavBar component={Feed} />
</Scene>
</Router>
);
};
export default Navigator;
now in your App.js file add this:
import Navigator from './src/Navigator.js';
export default class App extends Component<Props> {
render() {
return (
<Navigator />
);
}
}
now in your login.js when you click on login button write this:
import { Actions } from 'react-native-router-flux';
onLoginClick() {
Actions.feedscreen();
}
Thats it.. happy coding.
If you want to navigate to Feeds.js then navigate as
this.props.navigation.navigate('App');
not as
this.props.navigation.navigate('Main');
because your
export default SwitchNavigator({
Auth: AuthStack,
App: AppStack // here is your stack of Main
});
refer example
I came across the same issue few months ago. Thank god you have spent just 6 hours, i almost spent around 4 days in finding a solution for it.
Coming to the issue, Please note that in react-navigation you can either navigate to siblings or children classes.
So here, You have a swtichNavigator which contain 2 stack navigators (say stack 1 and stack 2), stack1 has feeds and stack2 has a tab navigator with login and signup.
Now you want to navigate from login.js to feeds.js(say file name is feeds.js). As mentioned already you can not navigate back to parent or grandparent. Then how to solve this issue?
In react native you have the privilege to pass params (screenprops) from parent to children. Using this, you need to store this.props.navigation of launchScreen into a variable and pass it to tab/login (check the tree structure). Now in the login.js use this variable to navigate.
You are simply passing the navigating privilege from parent to children.
Editing here:
<InnerTab screenProps={{rootNavigation : this.props.navigation }} />
Here, InnerTab is the tab navigator.
export const InnerTab = TabNavigator({
login: {
screen: login,
},
},
signup: {
screen: signup,
},
},
},
in login class, use const { navigate } = this.props.screenProps.rootNavigation;
Now you can use variable navigate.
I know its little tricky to understand but i have tried and it works.
Write your Navigator.js file as below,
import React from 'react'
import { NavigationContainer, useNavigation } from '#react-navigation/native'
import { createStackNavigator } from '#react-navigation/stack'
const SwitchNavigatorStack = () => {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName='{nameofscreen}' screenOptions={screenOptions}>
<Stack.Screen name='{nameofscreen}' component={{nameofscreen}}/>
<Stack.Screen name='{nameofscreen}' component={{nameofscreen}}/>
<Stack.Screen name='{nameofscreen}' component={{nameofscreen}}/>
<Stack.Screen name='{nameofscreen}' component={{nameofscreen}}/>
</Stack.Navigator>
</NavigationContainer>
)
}
export default SwitchNavigatorStack
Once, you are done with that change your App.js file to,
import SignedInStack from './navigation'
import React from 'react'
export default function App() {
return <SwitchNavigatorStack/>
}
After this, you are done with setting your project for navigating. In all the components where you want to add navigation feature make sure you use the navigation.navigate() (or) navigation.push() method. Also make sure you hook navigation constant by import useNavigation library. For example,
const Login = () => {
const navigation = useNavigation()
< Button title = 'Login' onPress={() => navigation.navigate('{nameofscreen}')} />
}
with this code snippet you can implement navigation between screens using #react-navigation/native and #react-navigation/stack
I would like to keep web page in memory so that when I click on back button (not the one on web browser) or on a routerlink, the HTML page instantly loads if I already visit it(because I have some data to load that I don't want to be reload).
I've seen a method with tabbed interface : https://www.w3.org/Style/Examples/007/target.en.html#tab1
but it is not adapted for my code architecture.
I'm using routerlinkin angular2 to navigate threw pages and a back button calling a function on click to go to the previous page.
I try to detail as far as I can so people can understand better my code architecture and the way routerlink method works.
Back button function (works independently from routerlink) :
goBack() {
window.history.back();
}
The router link method from page 1 to page 2:
page1.html :
<a[routerLink]="['PAGE2']"> go to page 2</a>
page1.ts component:
import { Router, ROUTER_DIRECTIVES } from '#angular/router-deprecated';
#Component({
selector: 'page1',
templateUrl: 'page1.html',
styleUrls: ['page1.css'],
directives: [ROUTER_DIRECTIVES]
})
main.ts :
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '#angular/router-deprecated';
#Component({
providers: [ROUTER_PROVIDERS]
})
#RouteConfig([
{ path: '/page2', name: 'PAGE2', component: Page2}]) //component representing class Page2 in page2.ts
Any idea to help me manage it is welcomed, thanks by advance !
Just cache the data in the service like explained in What is the correct way to share the result of an Angular 2 Http network call in RxJs 5?
There is currently no way to prevent the router from re-creating the component when you route away and back to the component.
Well, for those having the same problem, I think the best way to manage it is to map the data into a localStorage key like this :
localStorage.setItem('favoris',JSON.stringify(my_array)); //set my data array
array = JSON.parse(localStorage.getItem('key_name')); //get in array
And then ngOnInitin the class called by the router will call the initial function depending of localStorage key being true or not.
I just create the ionic project and I'm trying to make the sign in and sign up page and I just implement the HTML and CSS. but the problem is I can't change position between controllers.
The URL of controller is changes but the page is not changed. I was trying to import the correct module but I can't find the method.
Make sure in app.js, your templateURL , url is properly defined.
Most of the time it might be your 'URL' problem.
If you navigated from 'main', your next url should be : /main/success or something like that.
.state('tab.main', {
cache: false, //if you want to disable cache in ionic
url: '/main',
views: {
'tab-cases': {
templateUrl: 'templates/tab-main.html',
controller: 'MainCtrl'
}
}
})