Nuxt.js: How to use the same page with different URLs and switch the components to be displayed according to the URLHow do I modify the URL without reloading the page?How to use vue-infinite-loading in Nuxt.js (Vue.js)How to redirect to a different url inside the vue-router beforeRouteEnter hook?SyntaxError: Unexpected token < when trying to test with AVAHow to submit a form in Vue, redirect to a new route and pass the parameters?Nuxt.js – Nuxt generate does not pass onclick parameter to component on 404 pageUsing Vue Design System in Nuxt is throwing errors about export in system.jsRedirecting to the same page but switched language url, using Nuxt.js and Vue-i18nComputed Getter causes maximum stack size errorRun nuxt on https locally – problem with nuxt.config.js

std::tuple sizeof, is it a missed optimization?

How to encode a class with 24,000 categories?

How to find better food in airports

Count rook moves 1D

Can I sleep overnight at Stansted Airport

What drugs were used in England during the High Middle Ages?

Which is the best password hashing algorithm in .NET Core?

Does this bike use hydraulic brakes?

Why did the Joi advertisement trigger K?

How will the UK Commons debate tonight despite the prorogation?

Did Alan Turing's student Robin Gandy assert that Charles Babbage had no notion of a universal computing machine?

Case Studies and Real Problems for Teaching Optimization and Modelling

Finder/Terminal: Find files that contain less than 21 lines of text

Does immunity to damage from nonmagical attacks negate a rogue's Sneak Attack damage?

What does "se jouer" mean here?

What happens if I double Meddling Mage's 'enter the battlefield' trigger?

How do I stop making people jump at home and at work?

Question about derivation of kinematics equations

Travel to USA with a stuffed puppet

How many days for hunting?

Planet that’s 90% water or more?

Punishment in pacifist society

When is it legal to castle moving the rook first?

'Hard work never hurt anyone' Why not 'hurts'?



Nuxt.js: How to use the same page with different URLs and switch the components to be displayed according to the URL


How do I modify the URL without reloading the page?How to use vue-infinite-loading in Nuxt.js (Vue.js)How to redirect to a different url inside the vue-router beforeRouteEnter hook?SyntaxError: Unexpected token < when trying to test with AVAHow to submit a form in Vue, redirect to a new route and pass the parameters?Nuxt.js – Nuxt generate does not pass onclick parameter to component on 404 pageUsing Vue Design System in Nuxt is throwing errors about export in system.jsRedirecting to the same page but switched language url, using Nuxt.js and Vue-i18nComputed Getter causes maximum stack size errorRun nuxt on https locally – problem with nuxt.config.js






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















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?










share|improve this question






























    1















    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?










    share|improve this question


























      1












      1








      1


      0






      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?










      share|improve this question














      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?







      javascript vue.js nuxt.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 3:10









      xKxAxKxxKxAxKx

      3771 gold badge4 silver badges20 bronze badges




      3771 gold badge4 silver badges20 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          3







          +50









          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 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.






          share|improve this answer


































            1
















            All your paths have a common prefix users/. So you can use the pages/users/_.vue component to match any path starting with the users/ that was not matched to any other component.



            In this component you can examine $nuxt.$route.params.pathMatch to decide, which subcomponent to show. It will contain part of path after users/:



            <template>
            <div class="user">
            <div class="user__contents">
            <div class="user__contents__main">
            <UserInfo />
            <PostSection v-if="/^d+$/.test(path)" />
            <FollowSection v-else-if="/^d+/follow$/.test(path)" />
            <FollowerSection v-else-if="/^d+/follower$/.test(path)" />
            </div>
            </div>
            </div>
            </template>

            <script>
            export default
            computed:
            path()
            return this.$nuxt.$route.params.pathMatch;
            ,
            ,
            ...
            ;
            </script>






            share|improve this answer
































              0
















              Instead of one page , you can create individual page by setting your project directory structrue like this . It will more easy to handle and modify



              pages/
              --| users/
              -----| _id/
              --------| follow.vue
              --------| follower.vue
              -----| _id.vue





              share|improve this answer

























              • That is not an optimal solution. Having the same .vue page twice is difficult to maintain.

                – Bill Souvas
                Jul 15 at 6:26













              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389600%2fnuxt-js-how-to-use-the-same-page-with-different-urls-and-switch-the-components%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3







              +50









              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 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.






              share|improve this answer































                3







                +50









                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 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.






                share|improve this answer





























                  3







                  +50







                  3







                  +50



                  3






                  +50





                  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 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.






                  share|improve this answer















                  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 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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 17 at 18:02

























                  answered Jul 17 at 17:33









                  mutasakimutasaki

                  1,0276 silver badges16 bronze badges




                  1,0276 silver badges16 bronze badges


























                      1
















                      All your paths have a common prefix users/. So you can use the pages/users/_.vue component to match any path starting with the users/ that was not matched to any other component.



                      In this component you can examine $nuxt.$route.params.pathMatch to decide, which subcomponent to show. It will contain part of path after users/:



                      <template>
                      <div class="user">
                      <div class="user__contents">
                      <div class="user__contents__main">
                      <UserInfo />
                      <PostSection v-if="/^d+$/.test(path)" />
                      <FollowSection v-else-if="/^d+/follow$/.test(path)" />
                      <FollowerSection v-else-if="/^d+/follower$/.test(path)" />
                      </div>
                      </div>
                      </div>
                      </template>

                      <script>
                      export default
                      computed:
                      path()
                      return this.$nuxt.$route.params.pathMatch;
                      ,
                      ,
                      ...
                      ;
                      </script>






                      share|improve this answer





























                        1
















                        All your paths have a common prefix users/. So you can use the pages/users/_.vue component to match any path starting with the users/ that was not matched to any other component.



                        In this component you can examine $nuxt.$route.params.pathMatch to decide, which subcomponent to show. It will contain part of path after users/:



                        <template>
                        <div class="user">
                        <div class="user__contents">
                        <div class="user__contents__main">
                        <UserInfo />
                        <PostSection v-if="/^d+$/.test(path)" />
                        <FollowSection v-else-if="/^d+/follow$/.test(path)" />
                        <FollowerSection v-else-if="/^d+/follower$/.test(path)" />
                        </div>
                        </div>
                        </div>
                        </template>

                        <script>
                        export default
                        computed:
                        path()
                        return this.$nuxt.$route.params.pathMatch;
                        ,
                        ,
                        ...
                        ;
                        </script>






                        share|improve this answer



























                          1














                          1










                          1









                          All your paths have a common prefix users/. So you can use the pages/users/_.vue component to match any path starting with the users/ that was not matched to any other component.



                          In this component you can examine $nuxt.$route.params.pathMatch to decide, which subcomponent to show. It will contain part of path after users/:



                          <template>
                          <div class="user">
                          <div class="user__contents">
                          <div class="user__contents__main">
                          <UserInfo />
                          <PostSection v-if="/^d+$/.test(path)" />
                          <FollowSection v-else-if="/^d+/follow$/.test(path)" />
                          <FollowerSection v-else-if="/^d+/follower$/.test(path)" />
                          </div>
                          </div>
                          </div>
                          </template>

                          <script>
                          export default
                          computed:
                          path()
                          return this.$nuxt.$route.params.pathMatch;
                          ,
                          ,
                          ...
                          ;
                          </script>






                          share|improve this answer













                          All your paths have a common prefix users/. So you can use the pages/users/_.vue component to match any path starting with the users/ that was not matched to any other component.



                          In this component you can examine $nuxt.$route.params.pathMatch to decide, which subcomponent to show. It will contain part of path after users/:



                          <template>
                          <div class="user">
                          <div class="user__contents">
                          <div class="user__contents__main">
                          <UserInfo />
                          <PostSection v-if="/^d+$/.test(path)" />
                          <FollowSection v-else-if="/^d+/follow$/.test(path)" />
                          <FollowerSection v-else-if="/^d+/follower$/.test(path)" />
                          </div>
                          </div>
                          </div>
                          </template>

                          <script>
                          export default
                          computed:
                          path()
                          return this.$nuxt.$route.params.pathMatch;
                          ,
                          ,
                          ...
                          ;
                          </script>







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 17 at 18:42









                          Nina LisitsinskayaNina Lisitsinskaya

                          6982 silver badges12 bronze badges




                          6982 silver badges12 bronze badges
























                              0
















                              Instead of one page , you can create individual page by setting your project directory structrue like this . It will more easy to handle and modify



                              pages/
                              --| users/
                              -----| _id/
                              --------| follow.vue
                              --------| follower.vue
                              -----| _id.vue





                              share|improve this answer

























                              • That is not an optimal solution. Having the same .vue page twice is difficult to maintain.

                                – Bill Souvas
                                Jul 15 at 6:26















                              0
















                              Instead of one page , you can create individual page by setting your project directory structrue like this . It will more easy to handle and modify



                              pages/
                              --| users/
                              -----| _id/
                              --------| follow.vue
                              --------| follower.vue
                              -----| _id.vue





                              share|improve this answer

























                              • That is not an optimal solution. Having the same .vue page twice is difficult to maintain.

                                – Bill Souvas
                                Jul 15 at 6:26













                              0














                              0










                              0









                              Instead of one page , you can create individual page by setting your project directory structrue like this . It will more easy to handle and modify



                              pages/
                              --| users/
                              -----| _id/
                              --------| follow.vue
                              --------| follower.vue
                              -----| _id.vue





                              share|improve this answer













                              Instead of one page , you can create individual page by setting your project directory structrue like this . It will more easy to handle and modify



                              pages/
                              --| users/
                              -----| _id/
                              --------| follow.vue
                              --------| follower.vue
                              -----| _id.vue






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 28 at 4:21









                              デビットデビット

                              813 bronze badges




                              813 bronze badges















                              • That is not an optimal solution. Having the same .vue page twice is difficult to maintain.

                                – Bill Souvas
                                Jul 15 at 6:26

















                              • That is not an optimal solution. Having the same .vue page twice is difficult to maintain.

                                – Bill Souvas
                                Jul 15 at 6:26
















                              That is not an optimal solution. Having the same .vue page twice is difficult to maintain.

                              – Bill Souvas
                              Jul 15 at 6:26





                              That is not an optimal solution. Having the same .vue page twice is difficult to maintain.

                              – Bill Souvas
                              Jul 15 at 6:26

















                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389600%2fnuxt-js-how-to-use-the-same-page-with-different-urls-and-switch-the-components%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript