vue-customjs
v1.0.5
Published
A simple package to enable users to add custom JavaScript to a vue Component or project
Downloads
3
Readme
view package at https://www.npmjs.com/package/vue-customjs
vue-customjs
A simple package to enable users to add custom JavaScript to a vue Component or project
Initialisation
This is done by a simple require.
var customjs = require('vue-customjs');
Or when using ES6
import customjs from 'vue-customjs'
Available functions/Methods
Below are the available functions or methods for adding a custom javascript code to your vue component.
- add: Used to add custom JavaScript which is in the form of a string.
- addUrl: Used to add custom JavaScript from an external link, mostly libraries.
Examples
- add(jsCode): The add function takes only one parameter which is the JavaScript code(jsCode) in a form of a string.
Note:
Code added using this function is registered to the global scope. Meaning it not just called when the function in which it is defined is called. Hence you can register new Javascript codes to be added to the DOM. Code added using this function get to run always.
var jsCode = `
var a = 'I am a custom JavaScript code';
alert(a);
`;
customjs.add(jsCode);
- addUrl(url): This function takes one and only one parameter which is the url;
customjs.addUrl("https:www.cdn.com/library");
The Complete Example in a Vue Component
- add
<script>
var customjs = require('vue-customjs'); //importing the module into customjs var
export default {
data(){
return {
//nothing to do here
}
},
methods: {
buttonClick: ()=>{
var code = "alert('It is snowing')"; // The whole code you want to run
customjs.add(code); // adding code
}
}
}
</script>
- addUrl
<script>
var customjs = require('vue-customjs'); //importing the module into customjs var
export default {
data(){
return {
//nothing to do here
}
},
created: ()=>{
customjs.addPath('https:www.cdn.com/library'); //The url of the library you want to add
}
}
</script>