Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
103
rated 0 times [  110] [ 7]  / answers: 1 / hits: 13074  / 5 Years ago, thu, march 28, 2019, 12:00:00

I want to implement the following in Nuxt.js:



1.Use the same page with different URLs.

Specifically, I want to use /pages/users/_userId.vue with/users/{userId}, /users/{userId}/follow and /users/{userId}/follower.



I examined this and there were the following issues.

- https://github.com/nuxt/nuxt.js/issues/2693

But it was a little different from what I wanted to achieve.

I want to use the pass parameter for the query parameter.



2.Identify the components to display by path parameters

It would be quicker to have a look at the code here.



・/pages/users/_userId.vue`



<template>
<div class=user>
<div class=user__contents>
<div class=user__contents__main>
<UserInfo/>
<PostSection/> -> use if URL /users /{userId}
<FollowSection/> -> use if URL /users/{userId}/follow
<FollowerSection/> -> use if URL /users/{userId}/follower
</div>
</div>
</div>
</template>

<script>
import UserInfo from '~/components/organisms/users/UserInfo'
import PostsSection from '~/components/organisms/users/PostsSection'
import FollowSection from '~/components/organisms/users/FollowSection'
import FollowerSection from '~/components/organisms/users/FollowerSection'


...



What should I do to achieve these?


More From » vue.js

 Answers
6

As the displayed components depends on the current route, you can use the router.
Namely using the nuxtjs nested routes feature. (example of dynamic nested routes in nuxtjs docs)


pages/
--| users/
-----| _id/
--------| follow.vue // contains FollowSection
--------| follower.vue // contains FollowerSection
--------| index.vue // contains UserProfile
-----| _id.vue

// users/_id.vue
<template>
<div class="user">
<div class="user__contents">
<div class="user__contents__main">
<UserInfo>
<NuxtChild
:user="user"
@custom-event="customEventHandler"
/>
</div>
</div>
</div>
<template>

You can reuse the components as nested pages like that:


// pages/users/_id/follower.vue

<script>
// don't create a template for the section
import FollowSection from '~/components/organisms/users/FollowSection'
export default FollowSection
</script>

Note that you don't need to import the child components in _id.vue because nuxt configure vue-router to do so. Also that you can pass props and send events to them like normal components.


[#8256] Tuesday, March 26, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
margaritakristinak

Total Points: 502
Total Questions: 127
Total Answers: 98

Location: England
Member since Mon, May 17, 2021
3 Years ago
;