me-12 / single-spa-portal-example

Example project on how to combine multiple SPA's on a single Website
MIT License
407 stars 136 forks source link

Does Vue router work at all with single-spa? #40

Closed factoidforrest closed 6 years ago

factoidforrest commented 6 years ago

None of the examples seem to use the Vue router and I'm struggling to get it working.
I've tried history mode and hash mode, and different "base" URLs.

const router = new Router({
  base: '/vue-spa/',
  mode: 'history',
  routes,
});

Does some sort of a wrapper need to be written to make the Vue router compatible? Do we need to run Vue in an iframe(puke) to make the router work?

factoidforrest commented 6 years ago

I got it to work. I put the router into abstract mode where it doesn't touch or listen to the DOM.

I listen to hash change and hand the changes to Vue, after taking out the first part which is put in by single-spa.

 function inheritRoute() {
    let newRoute = window.location.hash.replace(/^#\/(vue-spa\/?|)/, ''); // in the future get the prefix programatically
    if (newRoute === '') newRoute = '/';
    console.log('newroute is ', newRoute);
    Vue.$router.push(newRoute);
  }

  inheritRoute();
  window.onhashchange = (e) => {
    console.log('Updating router from hashchange with e ', e, 'and hash ', window.location.hash);
    inheritRoute();
  };

Parameters and lazy loading and everything seem to be working fine. Vue is really the most extensible thing.