skip to Main Content

I am putting together a SPA using Vue with VueRouter. The site works and I am able to click the link and get to the menu items. I see in the url that the # still preceedes the link name. For example, if I click on Services in the menu, the url states https://sitename.com/#services
I am using mode: ‘history’ which is supposed to take care of this but, I believe I am missing something. If anyone can look over my code and help me out with this, I would sure appreciate it. Thank you.

In my root directory, I have the router.js

import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

function load(componentName) {
    return () => import(`@/components/${componentName}`);
}

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'Home',
            component: load('Home')
        }
        ,
        {
            path: '/services',
            name: 'Services',
            component: load('Services')
        },
        {
            path: '/portfolio',
            name: 'Portfolio',
            component: load('Portfolio')
        },
        {
            path: '/about',
            name: 'About',
            component: load('About')
        },
        {
            path: '/team',
            name: 'Team',
            component: load('Team')
        },
        {
            path: '/contact',
            name: 'Contact',
            component: load('Contact')
        },
        {
            path: '/privacy',
            name: 'Privacy',
            component: load('Privacy')
        },
        {
            path: '/success',
            name: 'Success',
            component: load('Success')
        }
    ]
});

export default router;

in my source directory and within components, I have the Nav.vue component
You can see I am using a conditional that says if the index of 0 is # etc. Which I thought was supposed to eliminate the hash but it does not.

Nav.vue

<template>
  <div>
    <nav class="navbar navbar-expand-lg navbar-dark fixed-top" id="mainNav">
      <div class="container">
        <a class="navbar-brand js-scroll-trigger" href="/">
          <span class="first-r">R</span>
          <span class="second-r">R</span>
          <span class="spark">Spark</span>
        </a>
        <button
          class="navbar-toggler navbar-toggler-right"
          type="button"
          data-toggle="collapse"
          data-target="#navbarResponsive"
          aria-controls="navbarResponsive"
          aria-expanded="false"
          aria-label="Toggle navigation"
        >
          Menu
          <i class="fas fa-bars"></i>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav text-uppercase ml-auto">
            <li class="nav-item" v-for="(linkObj, ind) in navList" :key="ind">
              <a
                class="nav-link js-scroll-trigger"
                :href="linkObj.path"
                v-if="linkObj.path.indexOf('#') === 0"
              >{{linkObj.name}}</a>
              <router-link
                class="nav-link"
                js-scroll-trigger
                v-else
                :to="linkObj.path"
              >{{linkObj.name }}</router-link>
            </li>
          </ul>
        </div>
      </div>
    </nav>
  </div>
</template>
<script>
export default {
  data: () => ({
    navList: [
      {
        name: "Services",
        path: "#services"
      },
      {
        name: "Portfolio",
        path: "#portfolio"
      },
      {
        name: "About",
        path: "#about"
      },
      {
        name: "Team",
        path: "#team"
      },
      {
        name: "Contact",
        path: "#contact"
      }
    ]
  })
};
</script>
<style lang="">
</style>

in SRC in my App.vue

<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
export default {};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>

main.js

import Vue from 'vue';
import App from './App.vue';
import router from '../router';
import VueResource from "vue-resource"
import './assets/css/style.css'
import './assets/vendor/bootstrap/css/bootstrap.min.css';
import './assets/vendor/fontawesome-free/css/all.min.css';


Vue.config.productionTip = false
Vue.use(VueResource);


new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

For the sake of space, I will only show the minimum of the Home.vue component

<template>
  <div>
    <Nav></Nav>
    <div>
      <header class="masthead">
        <div class="container">
          <div class="masthead-holder">
            <img src="../assets/img/RRLogo2.png" class="mt-5 logo" alt />
            <div class="intro-text">
              <div class="intro-lead-in">Welcome RR Spark Web Studio!</div>
              <div class="intro-heading text-uppercase">Hire Us For Your Next Web Project</div>
              <a
                class="btn tell-me btn-xl text-uppercase js-scroll-trigger"
                href="#services"
              >Tell Me More</a>
            </div>
          </div>
        </div>
      </header>
    </div>

    <div>
      <section class="page-section" id="services">
        <div class="container">
          <div class="row">
            <div class="col-lg-12 text-center">
              <h2 class="section-heading text-uppercase">Services</h2>
              <h3 class="section-subheading text-muted">Delivering your ideal website is what we do. 
              </h3>
            </div>
          </div>
          <div class="row text-center">
            <div class="col-md-4">
              <span class="fa-stack fa-4x">
                <i class="fas fa-circle fa-stack-2x icon-color"></i>
                <i class="fas fa-paint-brush fa-stack-1x fa-inverse"></i>
              </span>
              <h4 class="service-heading">Web Design</h4>
              <p
                class="text-muted text-justify"
              >We take the time to really know your company to get a better understanding of your goals, vision and where you want to go in order to create that perfect, unique website that will help you reach your specific target audience. Your site needs to be user friendly, functional and visually pleasing and that is where are designer comes in to ensure the design is exactly what you want.</p>
            </div>
            <div class="col-md-4">
              <span class="fa-stack fa-4x">
                <i class="fas fa-circle fa-stack-2x icon-color"></i>
                <i class="fas fa-globe fa-stack-1x fa-inverse"></i>
              </span>
              <h4 class="service-heading">Web Development</h4>
              <p
                class="text-muted text-justify"
              >Working hand in hand with our designer, we use the latest in web technology, such as HTML; CSS3; JavaScript, to bring you a site that is fast, SEO (Search Engine Optimization) friendly as well as highly responsive. Keeping up with the latest Standards from W3C, your project will be built for a superior user experience on mobile, tablet as well as PC.</p>
            </div>
            <div class="col-md-4">
              <span class="fa-stack fa-4x">
                <i class="fas fa-circle fa-stack-2x icon-color"></i>
                <i class="fas fa-laptop fa-stack-1x fa-inverse"></i>
              </span>
              <h4 class="service-heading">Content Management</h4>
              <p
                class="text-muted text-justify"
              >Keeping up with changes on your site in this fast-paced world means that your site will constantly need new and fresh content. Using a content management system (CMS), such as WordPress allows you, the user, the ability to quickly add new content whenever you wish, maintain and implement marketing campaigns and monitor analytics all from within the user dashboard. We provide either WordPress modifications or Custom theming solutions.</p>
            </div>
          </div>
          <div class="row mt-5">
            <div class="col-lg-12 text-center">
              <h2 class="section-heading text-uppercase">Ready? So Are We</h2>
              <div class="row">
                <div class="col-lg-6 p-5">
                  <h3
                    class="section-subheading text-muted mb-4"
                  >For a no-obligation quote, click the button below and tell us all about your project.</h3>
                  <a
                    class="btn tell-me btn-xl mt-0 mb-4 text-uppercase js-scroll-trigger"
                    href="#contact"
                  >CLICK ME</a>
                </div>
                <div class="col-lg-6 p-5">
                  <h3
                    class="section-subheading text-muted mb-4"
                  >If you don't like contact forms, ask us a question via WhatsApp using the button below.</h3>
                  <a
                    class="btn tell-me btn-xl mt-0 text-uppercase js-scroll-trigger"
                    href="https://api.whatsapp.com/send?phone=525517043338"
                    target="_blank"
                  >WhatsApp</a>
                </div>
              </div>
              <!--end row CTA-->
            </div>
          </div>
        </div>
      </section>
    </div>

<!--other sections-->



<div>
      <section class="page-section" id="contact">
        <div class="container">
          <div class="row">
            <div class="col-lg-12 text-center">
              <h2 class="section-heading text-uppercase">Contact Us</h2>
              <h3
                class="section-subheading text-white"
              >We are ready to get your project up and live.</h3>
            </div>
          </div>

          <div class="row">
            <div class="col-lg-12">
              <Contact></Contact>
            </div>
          </div>
        </div>
      </section>
    </div>

    <Footer></Footer>
  </div>
</template>
<script>
import Nav from "./Nav";
import Footer from "./Footer";
import Contact from "./Contact";
export default {
  name: "App",
  components: {
    Nav,
    Contact,
    Footer
  },
  data: () => ({
    portfolioJSON: [
      {
        linkToModal: "#portfolioModal1",
        image: require("../assets/img/portfolio/icoimanismall.jpg"),
        header: "Imani Luz Y Harmonía",
        caption: "Graphic Design"
      },
      {
        linkToModal: "#portfolioModal2",
        image: require("../assets/img/portfolio/icopsicliliansmall.jpg"),
        header: "Transformando Vidas",
        caption: "Web Development"
      },
      {
        linkToModal: "#portfolioModal3",
        image: require("../assets/img/portfolio/icopsicovintsmall.jpg"),
        header: "Viaje a tu Interior",
        caption: "Web Development"
      },
      {
        linkToModal: "#portfolioModal4",
        image: require("../assets/img/portfolio/algovisolutionsthumb.png"),
        header: "Algovi Solutions Landing Page",
        caption: "Web Development"
      },
      {
        linkToModal: "#portfolioModal5",
        image: require("../assets/img/portfolio/naplesdentalthumb.png"),
        header: "Naples Dental Clinic",
        caption: "Web Development"
      },
      {
        linkToModal: "#portfolioModal6",
        image: require("../assets/img/portfolio/xploreflythumb.png"),
        header: "Xplorefly Travel Agency",
        caption: "Web Development"
      }
    ]
  })
};
</script>
<style lang="">
</style>

Again, thank you for looking at this as I am new to VueJS and learning.

2

Answers


  1. it bcs your tag contain # in a href=”#”.

    its not part VueRouter, you need to remove it and make new function to scroll to specific area or div.

    let say:

    <a class="btn tell-me btn-xl text-uppercase js-scroll-trigger" href="#services">Tell Me More</a>
    

    You need to remove the ‘#’ inside the ‘href’ then add @click=”goToSection(‘yourAreaWantToJump’)”
    then need to add scroller to specific div. in this case u need to scroll to section ‘services’

    Login or Signup to reply.
  2. In Nav.vue your navList array containing the paths has a #. The paths has to be identical with the path you defined on your router.js. Try changing your array

    from:

    navList: [
          {
            name: "Services",
            path: "#services"
          },
          {
            name: "Portfolio",
            path: "#portfolio"
          },
          {
            name: "About",
            path: "#about"
          },
          {
            name: "Team",
            path: "#team"
          },
          {
            name: "Contact",
            path: "#contact"
          }
        ]
    

    to

    navList: [
          {
            name: "Services",
            path: "/services"
          },
          {
            name: "Portfolio",
            path: "/portfolio"
          },
          {
            name: "About",
            path: "/about"
          },
          {
            name: "Team",
            path: "/team"
          },
          {
            name: "Contact",
            path: "/contact"
          }
        ]
    

    if you can’t modify the array try modifing the string like:

    <router-link :to="linkObj.path.replace('#','/')">{{ linkObj.name }}</router-link>
    

    Conclusion

    For routing Navigation to render other components in the <router-view /> tag, try to use exclusive the <router-link to="/path"> tag. The <a href="/path"> tag will also navigate you but this will reload the page.

    If you want to navigate to anchors in your page you can still use the <a href="#anchorId">

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search