Skip to content

Instantly share code, notes, and snippets.

@tnorthcutt
Last active July 15, 2020 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnorthcutt/81f9b41b4d5bd1565d5258a1e00245b4 to your computer and use it in GitHub Desktop.
Save tnorthcutt/81f9b41b4d5bd1565d5258a1e00245b4 to your computer and use it in GitHub Desktop.
// in mixins/trailingSlash.js
export default (url) => {
return url.replace(/\/?$/, "/");
};
// in app.js
import trailingSlash from "./components/mixins/trailingSlash";
Vue.mixin({
methods: {
trailingSlash,
},
});
// in store/actions.js
import trailingSlash from "../components/mixins/trailingSlash";
let apiUrlBase = trailingSlash(process.env.MIX_API_URL);
@shai126
Copy link

shai126 commented Jul 14, 2020

Nice! Thinking out loud - would the other way round work too, so reuse isn't tied to an implementation detail of mixins?

// in helpers/trailingSlash.js
export default function trailingSlash (url) {
  return url.replace(/\/?$/, "/");
};

// in app.js
import trailingSlash from "./helpers/trailingSlash";
Vue.mixin({
  methods: {
    trailingSlash,
  }
});

// in store/actions.js
import trailingSlash from "../helpers/trailingSlash";
let apiUrlBase = trailingSlash(process.env.MIX_API_URL);

@tnorthcutt
Copy link
Author

Yeah, I like that better! Makes things tidier where it's used in Vuex. Nice idea 👏

@tnorthcutt
Copy link
Author

Updated with suggestion from @shai126 and converted to arrow function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment