In this article I will be explaining how we can use plugins in Vue to extend the Web Monetization functionality to our Vue App.
Plugins are the most simple and easy way to add global features to our app. They can be used to extend additional capabilities like routing, immutable stores, analytics and DOM manipulations.
So let's try implementing Web Monetization using plugins in your Vue App.
Steps
- Create a Web Monetization plugin.
// file: web-monetization.js
export default {
install(Vue) {
Vue.proptype.$webMonetizaton = this;
Vue.webMonetizaton = this;
if (typeof window !== "undefined") {
window.$webMonetizaton = this;
}
},
enableWebMonetization() {
if (!document.monetized) {
const monetizationTag = document.createElement("meta");
monetizationTag.name = "monetization";
monetizationTag.content = "payment_pointer";
document.head.appendChild(monetizationTag);
}
},
disableWebMonetization() {
const removeMonetizationTag = document.querySelector(
'meta[name="monetization"]'
);
removeMonetizationTag.remove();
},
webMonetizationEvents() {
document.monetization.addEventListener("monetizationstart", currentState);
},
currentState(event) {
//console.log(event);
}
};
- Register your plugin with your Vue App.
import Vue from "vue";
import { WebMonetizationPlugin } from "./web-monetization.js";
Vue.use(WebMonetizationPlugin);
- Use plugin in your app.
//to enable web monetization
this.$webMonetizaton.enableWebMonetization();
// to disable web monetization
this.$webMonetizaton.disableWebMonetization();
Link to code
You can find the code here
Top comments (2)
Soo, what does this webmonetizstion do, actually?
Web Monetization is a javascript browser API which support payments.
Here is the documentation link for more info 😄