|
1 | 1 | ---
|
2 |
| -contributors: [rigor789, jlooper, ikoevska] |
| 2 | +contributors: [vallemar, rigor789] |
3 | 3 | outdated: false
|
4 | 4 | ---
|
5 | 5 |
|
6 | 6 | # Upgrade Guide
|
7 | 7 |
|
8 |
| -> Estimated time for the upgrade: **10-20 minutes**. |
| 8 | +<!-- > Estimated time for the upgrade: **10-20 minutes**. --> |
9 | 9 |
|
10 |
| -To upgrade to NativeScript 8.0, start with a clean branch. |
| 10 | +## Upgrading from v2 to v3 |
11 | 11 |
|
12 |
| -Install latest NativeScript CLI: |
| 12 | +### Application Initialization Changes |
13 | 13 |
|
14 |
| -```bash |
15 |
| -$ npm install -g nativescript |
| 14 | +In Vue 2, the app was initialized like this: |
| 15 | + |
| 16 | +```ts |
| 17 | +import Vue from "nativescript-vue"; |
| 18 | +import Home from "./components/Home.vue"; |
| 19 | + |
| 20 | +new Vue({ |
| 21 | + render: (h) => h("frame", [h(Home)]), |
| 22 | +}).$start(); |
| 23 | +``` |
| 24 | + |
| 25 | +In Vue 3, you now use `createApp`: |
| 26 | + |
| 27 | +```ts |
| 28 | +import { createApp } from "nativescript-vue"; |
| 29 | +import Home from "./components/Home.vue"; |
| 30 | + |
| 31 | +const app = createApp(Home); |
| 32 | +app.start(); |
| 33 | +``` |
| 34 | + |
| 35 | +✅ **Key Changes:** |
| 36 | + |
| 37 | +- Use `createApp(Home)` instead of `new Vue()`. |
| 38 | +- The root `<Frame>` component should now be inside `Home.vue` (depending on your frame/navigation setup), not in the `createApp` function. |
| 39 | + |
| 40 | +[Example Implementation](https://github.com/nativescript-vue/nativescript-vue/blob/main/packages/template-blank/src/components/Home.vue#L33) |
| 41 | + |
| 42 | +### Navigation Changes in Vue 3 |
| 43 | + |
| 44 | +Navigation functions like `$navigateTo`, `$navigateBack`, and `$showModal` must now be **imported** instead of being accessed from `this`. |
| 45 | + |
| 46 | +```html |
| 47 | +<script lang="ts" setup> |
| 48 | + import { $navigateTo, $navigateBack, $showModal } from "nativescript-vue"; |
| 49 | + import MyComponent from "./components/MyComponent.vue"; |
| 50 | +
|
| 51 | + function navigate() { |
| 52 | + $navigateTo(MyComponent, { |
| 53 | + /* options */ |
| 54 | + }); |
| 55 | + } |
| 56 | +
|
| 57 | + function goBack() { |
| 58 | + $navigateBack(); |
| 59 | + } |
| 60 | +
|
| 61 | + function openModal() { |
| 62 | + $showModal(MyComponent, { |
| 63 | + /* options */ |
| 64 | + }); |
| 65 | + } |
| 66 | +</script> |
16 | 67 | ```
|
17 | 68 |
|
18 |
| -Verify you are on the latest version by running `ns -v`. |
| 69 | +> ✅ **Why the change?** |
| 70 | +> |
| 71 | +> Vue 3 now uses **composition API** and removes `$navigateTo` from the component instance. |
| 72 | +
|
| 73 | +> **Note** Vue3 also supports the options API, where these methods are still available on `this`, however we recommend using the composition API. |
19 | 74 |
|
20 |
| -In your project run |
| 75 | +### Plugin Registration |
21 | 76 |
|
22 |
| -```bash |
23 |
| -$ ns migrate |
| 77 | +Plugins are now registered using `registerElement` instead of modifying the Vue instance. |
| 78 | + |
| 79 | +#### **Before (Vue 2)** |
| 80 | + |
| 81 | +```ts |
| 82 | +import Vue from "nativescript-vue"; |
| 83 | + |
| 84 | +Vue.registerElement( |
| 85 | + "Gradient", |
| 86 | + () => require("nativescript-gradient").Gradient |
| 87 | +); |
24 | 88 | ```
|
25 | 89 |
|
26 |
| -This should get your project up-to-date with the latest dependencies. Some other things you may need to change: |
| 90 | +#### **Now (Vue 3)** |
| 91 | + |
| 92 | +```ts |
| 93 | +import { createApp, registerElement } from "nativescript-vue"; |
| 94 | +import Home from "./components/Home.vue"; |
| 95 | + |
| 96 | +registerElement("Gradient", () => require("nativescript-gradient").Gradient); |
27 | 97 |
|
28 |
| -If you still have imports from `tns-core-modules`, you will have to update them to use `@nativescript/core`, see the import reference guid in the NativeScript blog: https://blog.nativescript.org/nativescript-7-import-reference/index.html/ |
| 98 | +// or using import statements |
| 99 | +import { Gradient } from "nativescript-gradient"; |
| 100 | +registerElement("Gradient", () => Gradient); |
| 101 | + |
| 102 | +const app = createApp(Home); |
| 103 | +app.start(); |
| 104 | +``` |
| 105 | + |
| 106 | +> ✅ **Note** Some plugins export a Vue3 compatible plugin, that can be used with `.use()`, like `@nativescript-community/ui-collectionview/vue3`. Consult the plugin documentation and if it doesn't specify this, use `registerElement` normally. |
| 107 | +
|
| 108 | +```ts |
| 109 | +import { createApp } from "nativescript-vue"; |
| 110 | +import Home from "./components/Home.vue"; |
| 111 | +import CollectionView from "@nativescript-community/ui-collectionview/vue3"; |
| 112 | + |
| 113 | +const app = createApp(Home); |
| 114 | +app.use(CollectionView); |
| 115 | +app.start(); |
| 116 | +``` |
| 117 | + |
| 118 | +### ListView Changes |
| 119 | + |
| 120 | +1. Instead of `for="item in listOfItems"`, use `:items="items"` |
| 121 | +1. Instead of `if="condition"` us `:itemTemplateSelector="function"` |
| 122 | +1. Use `#default="{ item, index }"` inside `<template>` |
| 123 | + |
| 124 | +**Before (Vue 2)** |
| 125 | + |
| 126 | +```html |
| 127 | +<ListView for="item in listOfItems"> |
| 128 | + <v-template> |
| 129 | + <label :text="item.text" /> |
| 130 | + </v-template> |
| 131 | + |
| 132 | + <v-template if="item.odd"> |
| 133 | + <label :text="item.text" class="bg-red-500" /> |
| 134 | + </v-template> |
| 135 | +</ListView> |
| 136 | +``` |
| 137 | + |
| 138 | +**Now (Vue 3)** |
| 139 | + |
| 140 | +```html |
| 141 | +<script lang="ts" setup> |
| 142 | + const items = ref([ |
| 143 | + /* ... items... */ |
| 144 | + ]); |
| 145 | +
|
| 146 | + function itemTemplateSelector(item, index) { |
| 147 | + return index % 2 === 0 ? "default" : "odd"; |
| 148 | + } |
| 149 | +</script> |
| 150 | + |
| 151 | +<template> |
| 152 | + <ListView :items="items" :itemTemplateSelector="itemTemplateSelector"> |
| 153 | + <template #default="{ item, index }"> |
| 154 | + <label :text="item.text" /> |
| 155 | + </template> |
| 156 | + |
| 157 | + <template #odd="{ item, index }"> |
| 158 | + <label :text="item.text" class="bg-red-500" /> |
| 159 | + </template> |
| 160 | + </ListView> |
| 161 | +</template> |
| 162 | +``` |
| 163 | + |
| 164 | +🚀 **Bonus:** You can now strongly type `item` using TypeScript! |
| 165 | + |
| 166 | +```html |
| 167 | +<template |
| 168 | + #default="{ item, index }: { item: MyType, index: number }" |
| 169 | +></template> |
| 170 | +``` |
29 | 171 |
|
30 |
| -In your css, if you have imports that start with `~` you need to remove the tildes: |
| 172 | +Or, using the `ListItem` helper type: |
31 | 173 |
|
32 |
| -```css |
33 |
| -@import "~@nativescript/theme/..."; /* [!code --] */ |
34 |
| -@import "@nativescript/theme/..."; /* [!code ++] */ |
| 174 | +```html |
| 175 | +<template #default="{ item, index }: ListItem<MyType>"></template> |
35 | 176 | ```
|
0 commit comments