Skip to content

Cannot reuse components as views, using meta #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
unremarkablegarden opened this issue Jan 8, 2020 · 3 comments
Closed

Cannot reuse components as views, using meta #22

unremarkablegarden opened this issue Jan 8, 2020 · 3 comments
Labels
wontfix This will not be worked on

Comments

@unremarkablegarden
Copy link

'/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
Copy link
Member

rigor789 commented Jan 8, 2020

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

@unremarkablegarden
Copy link
Author

unremarkablegarden commented Jan 8, 2020

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
Copy link
Member

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:

routes[path].component.__path = path

Vue.mixin({
mounted() {
// attach the current path if set to the root element
if (this.$options.__path) {
this.$el.setAttribute('__path', this.$options.__path)
}
},
})

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

component: Object.assign({}, Posts)

@rigor789 rigor789 added the wontfix This will not be worked on label Mar 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Development

No branches or pull requests

2 participants