I need to detect if adblocker is being used on my site, from googling around the most common method of checking this is to create a ads.js or adverts.js file and then display a message if that has been blocked.
This file has been set up however I am having trouble accessing this file when it needs to be outside of the vue scope in a vue componenet, any suggestions?
You might need to register independent js file's to the window object, then inside your component, you can call the object or function. For example, if your ads.js contain like as follow snippet
function block(){
// console.log('i am gonnna show this ')
}
In your vue component
var newData = new Vue({
el: '#app',
data: {
demo:'',
},
methods: {
fetchData: function (data) {
window.block()
}
},
created: function () {
window.block()
}
})
Like above the code you can achieve.
Related
I am trying to write a function in Vue js who can auto-call on the live website at a particular time interval, which works is to check the database(google firebase cloud-firestore) collection/object.
This function does not depend on any events like (at-click, on-change, on-load, etc ).
It's completely independent, it's just called at a particular time interval
You can set a timer in your Vue object in main.js
new Vue({
el: '#app',
data: {},
created: function() {
setInterval(() => {
// call your API to check the database stuff
}, 1000)
}
});
But better approach is to create a dynamically scheduled background job in backend.
According to the docs, this is how you declare data in Vue:
data: {
name: 'Vue.js'
}
However, when I do that it doesn't work and an error shows in the console:
The "data" option should be a function that returns a per-instance value in component definitions.
I change it to the following and then it works fine:
data() {
return {
name: 'Vue.js',
}
}
Why do the Vue docs show the top bit of code when it doesn't work? Is there something wrong on my end?
Edit: This only happens when using components.
In a root Vue instance (which is constructed via new Vue({ . . . }), you can simply use data: { . . . } without any problems.
When you are planing to reuse Vue components using Vue.component(...) or using "template" tag, Use data attribute as a function.
Please review the corresponding section of the Vue.js documentation for more information regarding this problem
https://v2.vuejs.org/v2/guide/components.html#data-Must-Be-a-Function
You should declare data in Vue.js by doing
var app = new Vue({
el: '#app', //This is the container in which Vue will be in. The #app means the id of the container is app
data: {
}
});
It turns out you need to declare data in components different than when you set it on a Vue object.
Instead, a component’s data option must be a function, so that each instance can maintain an independent copy of the returned data object:
More: Vue docs
I face an issue where all my VueJS instances are being called even if the element is not on the page.
I have a mixin declared like this.
var mixin = {
methods: {
listEvents(parameters) {
return axios.get('/api/v1/events', {params: parameters})
},
listLocations(parameters) {
return axios.get('/api/v1/locations', {params: parameters})
},
}
}
And multiple instances, that use this mixing. If you ask why multiple instances, it is because I use VuejS in multiple pages. For example, I have a page that is listing events and a page that is listing locations. So all my methods are in the mixin, and then the instance that is made for the page is called because the page contains the element specified in the instance (for example : id="locations" and el: "#locations")
Example of one instance :
// Vue
new Vue({
el: '#locations',
delimiters: ['[[', ']]'],
mixins: [mixin],
data: {
locations: [],
loading: true,
error: false,
page: 1,
perPage: 20,
},
mounted: function () {
console.log("VUEJS 'locations' has been mounted")
this.init();
},
methods: {
init() {
...
...
But, whatever the page I am on, all the instances are called... I can see all the API calls, the leaflet maps are initiated but there is no container, ... it is a mess.
Am I missing something or misunderstanding totally ?
EDIT : I may add that all instances are in app.js and this app.js is included in all pages.
It sounds like you have some script that is loaded on every page, instantiating new Vue objects, regardless of whether or not the elements those Vues attach to are rendered.
Vue will create instances even if the element you are instantiating it on does not exist.
That being the case, you could simply check to see if the elements exist before you create the Vue.
const locations = document.querySelector("#locations")
if (locations) {
new Vue({
el: "#locations",
...
})
}
I couldn't manage to make it work. v-on:click event is not invoking the method on Vue instance. Here is the codes:
<div id="app">
<button class="btn btn-success" v-on:click="postEventData">
<i class="icon wb-share"></i> Publish
</button>
</div>
Vue instance:
var vm = new Vue({
el: '#app',
data: {
someData: 'fooBar'
},
methods: {
postEventData: function () {
axios.post('/foobar', vm._data);
}
}
});
Any help would be appreciated!
Change this axios.post('/foobar', vm._data);
to this: axios.post('/foobar', this.data);
I'm not seeing anything broken with your code.
That said, it would be better practice to use this to reference the Vue instance instead of vm. But, you would still need to reference the _data property of your Vue instance to get the data object (this.data is going to be undefined).
However, while you can reference your data object via this._data, it's a code smell. Your Vue instance's data properties are meant to be accessed individually directly off of this. Accessing the whole object breaks that paradigm.
If you are trying to submit { someData: 'fooBar' } in your post request, make a data property for that (say postData) and reference it via this.postData:
var vm = new Vue({
el: '#app',
data: {
postData: { someData: 'fooBar' }
},
methods: {
postEventData: function () {
axios.post('/foobar', this.postData);
}
}
});
Your template with v-on:click="postEventData" is fine.
Old question but this is what worked for me if someone else is stuck
button click event was actually trying to submit form since it was contained in a form element. So in order to make it call the method I had to do #click.prevent="myMethod()"
.prevent is the key. It acted as event.preventDefault()
I use the template data context inside a template's created function.
Template.temp.created = function() { console.log('this.data'); };
When I go to the page normally--i.e. click the link to the page--I see the console log the correct data object. When I hit the refresh button on the page, this.data is null.
Why?
Also, I am using iron-router to set the data context:
...
this.route('temp', {
...
data: function() { return MyCollection.findOne(someId); },
...
}
...
If you want to wait until data come then use waitOn.
this.route('temp', {
waitOn:function(){
return this.subscribe("nameOfPublishFunction");
},
data: function() { return MyCollection.findOne(someId); },
...
}
Remember to activate loading hook (thanks #Peppe L-G):
Router.onBeforeAction("loading");
IronRouter docs #waitOn
Update
Here you can find sample meteor app with iron:router package which shows how turning loading hook on and off ( Router.onBeforeAction("loading")) changes availability of data to created and rendered methods.