Skip to content

Commit 78dd826

Browse files
Merge pull request #8 from nativescript-vue/ns-vue-navigator
Integrate Nativescript-vue-navigator
2 parents 90795ef + c72b4a6 commit 78dd826

File tree

4 files changed

+63
-38
lines changed

4 files changed

+63
-38
lines changed

generator/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ module.exports = async (api, options, rootOptions) => {
114114
//
115115
}
116116

117+
if (rootOptions.router) {
118+
api.extendPackage({
119+
dependencies: {
120+
'nativescript-vue-navigator': '^0.0.3'
121+
}
122+
});
123+
}
124+
117125
if (api.hasPlugin('typescript')) {
118126
api.extendPackage({
119127
dependencies: {},

generator/templates/simple/src/App.vue

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@
5656
<script>
5757
<%_ if (!rootOptions.router) { _%>
5858
import HelloWorld from '~/components/HelloWorld';
59-
<%_ } else { _%>
60-
import Home from '~/views/Home';
61-
import About from '~/views/About';
6259
<%_ } _%>
6360

6461
<%_ if (!rootOptions.router) { _%>
@@ -84,10 +81,13 @@
8481
methods: {
8582
<%_ if (rootOptions.router) { _%>
8683
goToHomePage() {
87-
this.$navigateTo(Home);
84+
this.goTo('home');
8885
},
8986
goToAboutPage() {
90-
VUE_APP_MODE == 'web' ? this.$router.push('about') : this.$navigateTo(About);
87+
this.goTo('about');
88+
},
89+
goTo(route) {
90+
VUE_APP_MODE === 'web' ? this.$router.push(route) : this.$navigator.navigate(route);
9191
}
9292
<%_ } _%>
9393
}
@@ -99,9 +99,6 @@
9999
import { Component, Vue } from 'vue-property-decorator';
100100
<%_ if (!rootOptions.router) { _%>
101101
import HelloWorld from '~/components/HelloWorld.vue';
102-
<%_ } else { _%>
103-
import Home from '~/views/Home.vue';
104-
import About from '~/views/About.vue';
105102
<%_ } _%>
106103

107104
<%_ if (!rootOptions.router) { _%>
@@ -125,12 +122,17 @@
125122
<%_ } _%>
126123

127124
<%_ if (rootOptions.router) { _%>
125+
128126
public goToHomePage() {
129-
Vue.prototype.$navigateTo(Home);
127+
this.goTo('home');
130128
}
131129

132130
public goToAboutPage() {
133-
VUE_APP_MODE === 'web' ? this.$router.push('about') : Vue.prototype.$navigateTo(About);
131+
this.goTo('about');
132+
}
133+
134+
public goTo(route) {
135+
VUE_APP_MODE === 'web' ? this.$router.push(route) : Vue.prototype.$navigator.navigate(route);
134136
}
135137
<%_ } _%>
136138
}
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
1-
---
2-
extend: '@vue/cli-service/generator/template/src/main.js'
3-
replace:
4-
- !!js/regexp /import Vue from 'vue'/
5-
- !!js/regexp /import App from './App.vue'/
6-
- !!js/regexp /Vue.config.productionTip = false/
7-
- !!js/regexp /h => h\(App\),/
8-
- !!js/regexp /}\)\.\$mount\('#app'\)/
9-
---
10-
11-
<%# REPLACE %>
121
import Vue from 'nativescript-vue';
13-
<%# END_REPLACE %>
2+
<%_ if (rootOptions.router) { _%>
3+
import Navigator from 'nativescript-vue-navigator'
4+
<%_ } _%>
145

15-
<%# REPLACE %>
166
import App from './App.vue';
17-
<%# END_REPLACE %>
7+
<%_ if (rootOptions.router) { _%>
8+
import { options } from './router';
9+
10+
// adapt vue-router routes to nativescript-vue-navigator
11+
const routes = options.routes.reduce((data, route) => {
12+
data[route.name] = {
13+
component: route.component
14+
}
15+
return data
16+
}, {});
17+
18+
Vue.use(Navigator, { routes });
19+
<%_ } _%>
1820

19-
<%# REPLACE %>
2021
// Set the following to `true` to hide the logs created by nativescript-vue
2122
Vue.config.silent = false;
2223
// Set the following to `false` to not colorize the logs created by nativescript-vue
2324
// disabled in template due to typing issue for Typescript projects....NEEDS TO BE FIXED
2425
// Vue.config.debug = true;
25-
<%# END_REPLACE %>
26-
27-
<%# REPLACE %>
28-
(h) => h('frame', [h(App)]),
29-
<%# END_REPLACE %>
3026

31-
<%# REPLACE %>
27+
new Vue({
28+
render: h => h('frame', [h(App)]),
3229
}).$start();
33-
<%# END_REPLACE %>

generator/templates/simple/src/router.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ extend: '@vue/cli-service/generator/router/template/src/router.js'
33
replace:
44
- !!js/regexp /import Vue from 'vue'/
55
- !!js/regexp /import Router from 'vue-router'/
6-
- !!js/regexp /Vue.use\(Router\)/
7-
- !!js/regexp /import Home from './views/Home.vue'/
8-
- !!js/regexp /'./views/About.vue'\)/
6+
- !!js/regexp /Vue.use\(Router\)/
7+
- !!js/regexp /export default new Router\(\{/
8+
- !!js/regexp /import Home from '\./views/Home.vue'/
9+
- !!js/regexp /\(\) => import(.*)\.\/views\/About\.vue'\)/
10+
- !!js/regexp /(\s+)\/\/ (.*)/
11+
- !!js/regexp /(\s+)\/\/ (.*)/
12+
- !!js/regexp /(\s+)\/\/ (.*)/
913
- !!js/regexp /\}\)/
1014
---
1115

@@ -21,14 +25,29 @@ import Router from 'vue-router';
2125
Vue.use(Router);
2226
<%# END_REPLACE %>
2327

28+
<%# REPLACE %>
29+
export const options = {
30+
<%# END_REPLACE %>
31+
2432
<%# REPLACE %>
2533
import Home from '~/views/Home.vue';
34+
import About from '~/views/About.vue';
2635
<%# END_REPLACE %>
2736

2837
<%# REPLACE %>
29-
'~/views/About.vue'),
38+
About,
3039
<%# END_REPLACE %>
3140

3241
<%# REPLACE %>
33-
});
34-
<%# END_REPLACE %>
42+
<%# END_REPLACE %>
43+
44+
<%# REPLACE %>
45+
<%# END_REPLACE %>
46+
47+
<%# REPLACE %>
48+
<%# END_REPLACE %>
49+
50+
<%# REPLACE %>
51+
};
52+
export default new Router(options);
53+
<%# END_REPLACE %>

0 commit comments

Comments
 (0)