nativescript-vue / nativescript-vue-navigator

A simple router for NativeScript-Vue, built on top of $navigateTo to simplify routing from within components
MIT License
98 stars 10 forks source link

Cannot reuse components as views, using meta #22

Closed unremarkablegarden closed 4 years ago

unremarkablegarden commented 4 years ago

'/news': { component: Posts, meta: { path: 'news', title: 'News', } },

I'm trying to use one component, Posts.vue, for multiple views. But all the views turn in to the last referenced in the navigator setup. The content in Posts is loaded using the meta.path.

Seems I have to make duplicates of this Posts component for it to work. Weird.

rigor789 commented 4 years ago

Share your code - can't help if I don't know how you are using it.

unremarkablegarden commented 4 years ago

The code below works perfectly fine if I copy it to two components, News.vue and Seminare.vue. Both have the same layout, etc. Just different content. When I try to use 'meta' in the navigator to load different content in a single component (Posts), I get the same content in both (the last one referenced in the navigator config). And as I said, leaving everything as it is, as long as it's two separate components it works. You just can't re-use a component in the navigator.

Not sure if this is a limitation of navigator or of NativeScript Vue...

routes.js:

'/news': {
        component: Posts,
        meta: {
            path: 'news',
            needsAuth: true,
            title: 'News',
        }
    },
    '/seminare': {
        component: Posts,
        meta: {
            path: 'seminare',
            needsAuth: true,
            title: 'Seminare',
        }
    },

Posts.vue

computed:

path: function () {
            return this.$navigator.route.meta.path
        },

template: <StackLayout v-for='(post, index) in resources[path]' :key='index' @onTap='viewPost(post)'>

Here resources[path] works fine if they're in different component files.

rigor789 commented 4 years ago

Just checked this, and correct - components can't be reused for multiple routes.

I don't see an easy way to fix this, as the components have to keep track what route they belong to: https://github.com/nativescript-vue/nativescript-vue-navigator/blob/99f9e36bd5f06fa57a4958a1fa0bad4b79aa6a98/index.js#L7

https://github.com/nativescript-vue/nativescript-vue-navigator/blob/99f9e36bd5f06fa57a4958a1fa0bad4b79aa6a98/index.js#L10-L17

However, it's possible to work around the issue with cloning the component. For example

component: Object.assign({}, Posts)