Skip to content

Instantly share code, notes, and snippets.

@bfu4
Last active January 30, 2021 04:07
Show Gist options
  • Save bfu4/ce2fe4b7ed33f2fc4fa1e776db262986 to your computer and use it in GitHub Desktop.
Save bfu4/ce2fe4b7ed33f2fc4fa1e776db262986 to your computer and use it in GitHub Desktop.
how i fought vue-router with the help of 10 blog posts.

routing.md

at this point, i've made github gist my archive of shitty solutions, haven't i?

Okay, seriously, I have no idea why or HOW it was so hard for me to get a decent routing system working in vue 2.0, but I did - and to be quite honest, I'm only dropping this here so that I can find it again in like .. a week.

Behold!

This is the shitshow of a router design I put together, using more posts than I can name, but I'll drop the most helpful.

Actual code:

// Here are the example routes I "routed". The components are respective, blah blah blah
export const routes = [
    { path: "/", component: App },
    { path: "/meow", component:  Meow }
]

/**
 * Get a component by path
 *
 * @param path path to get component by
 */
export function getComponent(name : string) : Component {
    // This probably sucks as practice .. however, I don't know vue practice, and I didn't want to deal with null
    let component!: Component;
    routes.map(route => {
        if (route.path.localeCompare(name) == 0) {
            component = route.component;
        }
    });
    return component;
}

// create router
const router = new VueRouter({
    routes
});

// fuckin export that bitch
export default router;

And what about the main.js (or .ts ..)

// It literally comes down to creating the new Vue, shoving the router in it, and rendering it as so.
const app = new Vue({
    router,
    render: function(createElement) {
        return createElement(getComponent(window.location.pathname));
    }
}).$mount('#app');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment