Silently update url without triggering route in vue-router
The Vue router is either on or off, so you cannot "make it not detect certain urls". That said, Vue does re-use components if it doesn't need to destroy them and allows you to alias urls. This allows you to have the urls from your iframe application as aliases in your route, and keep the same component alive during that time, preventing your iframe from getting reloaded.
// router.js
import Comp1 from "./components/Comp1";
import Comp2 from "./components/Comp2";
export default [
{
path: "/route1",
component: Comp1,
alias: ["/route2", "/route3", "/route4"]
},
{
path: "/otherroute",
component: Comp2
}
];
// main.js
import Vue from "vue";
import App from "./App";
import VueRouter from "vue-router";
import routes from "./router";
Vue.config.productionTip = false;
Vue.use(VueRouter);
const router = new VueRouter({
routes
});
/* eslint-disable no-new */
new Vue({
el: "#app",
router,
components: { App },
template: "<App/>"
});
In this example, route1
, route2
, route3
and route4
will all be treated as if route1
has to be loaded, causing your component Comp1
to stay alive.
Without reloading the page or refreshing the DOM, history.pushState
can do the job.
For example: add this method in your component or elsewhere to do that:
addHashToLocation(params) {
history.pushState(
{},
null,
this.$route.path + '#' + encodeURIComponent(params)
)
}
Then anywhere in your component you can call addHashToLocation('/my/new/path')
to push query params to the window.history
stack.
To add query parameters to current location without pushing a new history entry, use history.replaceState
instead.
Should work with Vue 2.6.10 and Nuxt 2.8.1. Be careful with this method, as Vue Router does not know that the URL has changed.