nativescript-vue-navigator
1.2.0 • Public • PublishedNativeScript-Vue-Navigator
NativeScript-Vue-Navigator is a simple router implementation that is suitable for NativeScript-Vue.
Quick Start
$ npm install --save nativescript-vue-navigator
// main.jsimport Vue from 'nativescript-vue'...+ import Navigator from 'nativescript-vue-navigator'+ import {routes} from './routes'+ Vue.use(Navigator, { routes })new Vue({- render: h => h('frame', App),+ render: h => h(App),}).$start()
// routes.jsimportHomePagefrom'./components/HomePage'importLoginPagefrom'./components/LoginPage'exportconstroutes={'/home':{component:HomePage,},'/login':{component:LoginPage,},}
// App.vue<template>+ <Navigator :defaultRoute="isLoggedIn ? '/home' : '/login'"/></template>
Attaching extra data to a route
// routes.jsimport HomePage from './components/HomePage'import LoginPage from './components/LoginPage'export const routes = { '/home': { component: HomePage,+ // we are using `meta` as a good practice, but you are free to use something else+ meta: { needsAuth: true } }, '/login': { component: LoginPage,+ meta: { needsAuth: false } },}
<!-- anywhere in your templates--><Label :text="$navigator.route.meta.needsAuth" />
// or in any vue componentexportdefault{methods:{doStuff(){if(this.$navigator.route.meta.needsAuth){// do stuff}}}}
Getting the current path
// logs the current path in the default navigatorconsole.log(this.$navigator.path)// logs the current path in the second navigator (See Multiple Navigators section for more details)console.log(this.$navigator.paths.second)
Navigating
This package provides 2 methods for navigation,$navigator.navigate
and$navigator.back
$navigator.navigate(to, options)
is used for all forward navigation
to
is the path to navigate to (ex.:/home
)options
is an optional object, which accepts all options supported byManual Routing
For example, given you are on a Login page, and successfully log in you would navigate to the Home page with
this.$navigator.navigate('/home',{clearHistory:true})
Note that we usedclearHistory: true
to prevent the back button from going back to the login page.
$navigator.back(options, backstackEntry)
is an alias to$navigateBack
Multiple Navigators
It is possible to use multiple<Navigator>
elements by providing each new Navigator with a uniqueid
.
<template><!-- this is the default navigator and can omit the id--> <Navigator /><!-- shows the current path of the default navigator--> <Label :text="$navigator.path" /><!-- this is the second navigator and it MUST have a unique id--> <Navigatorid="second" /><!-- shows the current path of the second navigator--> <Label :text="$navigator.paths.second" /></template><script>exportdefault { methods: {someMethod() { //navigatethedefaultNavigatorthis.$navigator.navigate('/new-path') //navigatetheseconddefaultNavigatorbyspecifyingtheframeoptionthis.$navigator.navigate('/new-path', { frame:'second' }) //navigatebackthedefaultNavigatorthis.$navigator.back() //navigatebackthesecondNavigatorthis.$navigator.back({ frame:'second' }) } } }</script>
Navigator Modals
typeModalOptions={id:string}&ShowModalOptionsthis.$navigator.modal(path:string,options:ModalOptions);
The default id for modal navigators ismodalNavigator
but can be changed by passing anid
inside the ModalOptions.
// use the default id for the modalthis.$navigator.modal('/path',{fullscreen:true})// to navigate the modal to '/other'this.$navigator.navigate('/other',{frame:'modalNavigator'})// use a different id for the modalthis.$navigator.modal('/path',{fullscreen:true,id:'myModal'})// to navigate the myModal modal to '/other'this.$navigator.navigate('/other',{frame:'myModal'})
Package Sidebar
Install
npm i nativescript-vue-navigator
Weekly Downloads
37
Version
1.2.0
License
MIT
Unpacked Size
10.2 kB
Total Files
5