javascript - 将 webSDK 与 jQuery Inside Vue 组件集成?
问题描述
我正在尝试将来自https://www.pollfish.com/docs/webplugin的 webSDK 集成到我们的 Vue 应用程序中。
理想情况下,我只想在一个组件中加载 jquery。
我编写了以下代码,但是当我单击按钮时它不起作用。
这是一个不使用 Vue https://github.com/pollfish/webplugin-rewarded-example/blob/master/index.html但在本地运行的工作代码示例。
我没有错误,我可以console.log(Pollfish)
在showFullSurvey
方法中。
我的代码是:
<template>
<div class="container" v-if="isFreePlan">
<h2>Remove ads and save unlimited projects for 5 days</h2>
<button @click="showFullSurvey">Take {{lengthOfInteraction}} Survey Now</button>
</div>
</template>
<script>
import { mapGetters } from 'vuex';
export default {
data() {
return {
surveyAvailable: false,
lengthOfInteraction: ''
}
},
methods: {
showFullSurvey() {
Pollfish.showFullSurvey();
console.log('show survey')
}
},
mounted() {
const pollFishConfig = {
api_key: "api-key",
debug: process.env.NODE_ENV === 'production' ? false : true,
ready: () => {},
uuid: this.userId,
surveyAvailable: onSurveyAvailable,
surveyNotAvailable: onSurveyNotAvailable,
surveyCompletedCallback: onSurveyCompleted,
userNotEligibleCallback: onUserDisqualified
};
console.log('POllfish config');
const onSurveyAvailable = (data) => {
console.log('SUrvey Available');
};
const onSurveyNotAvailable = () => {
console.log('SUrvey Not Available');
};
const onSurveyCompleted = () => {
console.log('SUrvey Completed');
};
const onUserDisqualified = () => {
console.log('USer Disqualified');
};
this.addJQuery;
this.addPollFishSDK;
},
computed: {
...mapGetters("session", ['userId']),
...mapGetters("account", ["isFreePlan"]),
addJQuery() {
const url = 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js';
if(document.querySelector(`script[src='${url}']`)){ return; }
let jquery = document.createElement('script');
jquery.setAttribute('src', url);
document.body.appendChild(jquery);
console.log('jquery script')
},
addPollFishSDK() {
const url = 'https://storage.googleapis.com/pollfish_production/sdk/webplugin/pollfish.min.js';
if(document.querySelector(`script[src='${url}']`)){ return; }
let pollFishSdk = document.createElement('script');
pollFishSdk.setAttribute('src', url);
document.body.appendChild(pollFishSdk);
console.log('pollfish script')
}
}
}
</script>
解决方案
为了将我们的网络插件集成到您的 Vue.js 应用程序中,您需要pollfishConfig
在窗口中设置对象。请注意对象的名称与以下示例完全相同。
window.pollfishConfig = {
api_key: "api-key",
debug: process.env.NODE_ENV === 'production' ? false : true,
ready: () => {},
uuid: this.userId,
surveyAvailable: onSurveyAvailable,
surveyNotAvailable: onSurveyNotAvailable,
surveyCompletedCallback: onSurveyCompleted,
userNotEligibleCallback: onUserDisqualified
};
此外,根据您的示例,您需要确保首先加载 jQuery 库并且可用于我们的 WebPlugin SDK。所以你需要处理事件onload
。基于您的代码的示例解决方案如下:
const addScript = (url, onLoad) => {
const scriptExists = document.querySelector(`script[src='${url}']`);
if (!scriptExists) {
let script = document.createElement('script');
document.body.appendChild(script);
script.onload = () => {
onLoad && onLoad();
}
script.src = url;
}
}
addScript('https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', () => {
addScript('https://storage.googleapis.com/pollfish_production/sdk/webplugin/pollfish.min.js')
});
推荐阅读
- c# - IronPython 抛出以下异常:无法转换 Func 类型的对象
功能 - ansible - 如何使用循环加速任务
- java - 如何使用 Java 打开以 .tif 图像文件形式给出的非常大的整个幻灯片图像?
- excel - If Statement for Error Checking is not working
- docusignapi - 有没有办法删除在发件人视图上删除/修改上传到 DocuSign 的文档的功能?
- linux - Linux - grep -e 在从命令调用 script.sh 时有效,但在从 crontab 调用 script.sh 时失败
- android - 预览隐藏的实时视频时获得绿色图像
- javascript - 使用类数组的更简单方法
- react-native - React Native:删除 Expo Cookie
- jquery - 在一行中将多个按钮插入页面