bshiluk / vue-wordpress

Use Vue.js and the WP REST API to build WordPress themes as SPAs with dynamic routing, HMR for development, SEO enabled, and SSR capable. Demo:
http://vue-wordpress-demo.bshiluk.com
491 stars 112 forks source link

Using <router-link>? #19

Closed psntr closed 4 years ago

psntr commented 4 years ago

I wanted to know why not using the <router-link>? We could use the benefit that comes with it, as such as the class="active" on the current & clicked nav item.

How do other folks deal with the class="active" on their theme?

omaratta2 commented 4 years ago

I'd struggled sometimes with that, then I used any way. Why the theme doesn't use router-link? Let's say that you have an article with an internal link, like a link to about page for example. you have two options.

  1. Use but then the 'to' attribute will hold the whole URL not just the route and it will fail.
  2. use tag and it will reload the entire page so the app isn't SPA anymore!

This theme solves the problem using internal link deligation which is explained in detail in the readme, yet I find confusing & especially for the "active" class.

Another solution Using this method could be helpful, you will need to pass any URL of this type to that method first

function routeFromLink(url){
    const siteUrl = this.$store.state.site.url
    if(url.includes(siteUrl)) return url.replace(siteUrl, '')
    else return '/'
}

Then in the navbar:

<route-link v-for="item in menu" :to="routeFromLink(item.url)">{{ item.content }}</route-link>
psntr commented 4 years ago

Thank you, that's a clever way to solve that. Also thank you for explaining further reason, I didn't think about the internal links. Hope someone will find this helpful!