gpt4 book ai didi

javascript - Vue.js - 如何访问外部 Javascript/Jquery 函数

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:44 26 4
gpt4 key购买 nike

我正致力于在 Vue.js 中创建灯箱/模态组件。我已经得到它的大部分工作,现在我想使用另一个文件中的一些现有函数,这些函数是用 Javascript/Jquery 编写的。现在在 Vue 中重写这些任务太大了,所以我们现在只需要按原样使用它们。但我挂断了如何访问它们。我不确定是否需要像混合或其他不同的方式导入它。

按照现在的代码,我收到错误:[Vue warn]: Property or method "lightbox"is not defined on the instance but referenced during render.

这是我的 Vue 组件中的相关代码:

<template>
...
<i
class="fa fa-info-circle"
name="moreInfo"
aria-hidden="true"
@click="lightbox.showBasketHelpLightbox('servicePlan', true, 3)"
/>
...
</template>
<script>
...
import lightbox from '../../../legacy/static/js/lightbox';

export default {
name: "AddToCartModal",
components: {.....},
mixins: [....., lightbox],
....
}
</script>

以及来自lightbox.js的相关代码

const accessories = [];
let selectedServicePlanSku = selectedServicePlanSku || undefined;

var APPLICATION_STATE = APPLICATION_STATE || (function () {
try {
return JSON.parse(__VUEX_STATE__);
} catch (e) { }
return {uri: {}, machine: {}};
})();

var staticURL = staticURL || APPLICATION_STATE.uri.application;
var staticMediaURL = staticMediaURL || APPLICATION_STATE.uri.static;
var INTERNAL = INTERNAL || APPLICATION_STATE.machine.internal ? "true" : "false";
var KIOSK = KIOSK || APPLICATION_STATE.machine.kiosk ? "true" : "false";
// whole bunch of other functions
function showBasketHelpLightbox(content, returnToCartLightbox, additionalParam, returnToWishlistLightbox){
let helpTextCategoryId;
if (content === "rebates") {
helpTextCategoryId = 13170;
} else if (content === "projects") {
helpTextCategoryId = 1508847718230;
} else if (content === "servicePlan") {
if(additionalParam === 3){
helpTextCategoryId = 12811;
} else if (additionalParam === 4){
helpTextCategoryId = 12812;
} else if (additionalParam === 6){
helpTextCategoryId = 12813;
} else if (additionalParam === 8){
helpTextCategoryId = 12927;
} else if (additionalParam === 10 || additionalParam === 12 || additionalParam === 14
|| additionalParam === 18 || additionalParam === 20 || additionalParam === 22){
helpTextCategoryId = 12814;
} else if (additionalParam === 16){
helpTextCategoryId = 12815;
} else if(additionalParam === 24){
helpTextCategoryId = 1483373312360;
} else if(additionalParam === 26) {
helpTextCategoryId = 1483373312361;
}
}

closeBasketLightboxPopUp();

$(document).ready(function(){
$("div#basketHelpLightbox-panel").show(function(){
$("#basketHelpLightbox-panel").fadeIn(300);
});
$("div#basketHelpLightbox").fadeTo(300, .6);
$("div#skipTab").click(function(){
$("#basketHelpLightbox, #basketHelpLightbox-panel").fadeOut(300);
});
});
jQuery("div#basketHelpLightboxPopUp").empty();
jQuery("div#basketHelpLightboxPopUp").show();

if(content === "learnMore"){

let lightboxmsg = '<div id="basketHelpLightboxPopUp"" class="" style="display: block;">';
lightboxmsg += '<div id="popupMainDiv">';
lightboxmsg += '<div id="flexibleLightboxPopUp"> <center> <span id="descriptiveTitle"> text </span> <span id="skipTabitemTempDisclaimFlexLightbox" onclick="closeBasketHelpLightbox(' + returnToCartLightbox + ', ' + returnToWishlistLightbox + ')" class="btn_SF btnClose gradient rightFloat" tabindex="0" onkeypress="checkClickOnEnterEvent(event, this);"><strong>X</strong></span> </center></div>';
lightboxmsg += ' <div id="bodyOfPopup" style="padding:5px;z-index:5501;"><div style="padding:10px;">more text</div></div></div></div>';

jQuery('#basketHelpLightboxPopUp').replaceWith(lightboxmsg);
} else{
let text = '<div id="basketHelpLightboxheaderPopUp">';
text += '<span id="descriptiveTitle">Additional Information</span>';
text += '<a href="javascript:void(0)" onclick="closeBasketHelpLightbox(' + returnToCartLightbox + ', ' + returnToWishlistLightbox + ');return false;" class="btn_SF btnClose gradient rightFloat"><strong>X</strong></a>';
text += '</div>';
text += "<iframe scrolling='auto' id='basketHelpLightboxIframe' src='content.html?cid=" + helpTextCategoryId + "'></iframe>";
jQuery('#basketHelpLightboxPopUp').append(text);
jQuery('#basketHelpLightboxPopUp .btnClose').focus();

$(document).ready(function(){
$("div#basketHelpLightbox-panel").show(function(){
$("#basketHelpLightbox-panel").fadeIn(300);
$("div#basketHelpLightbox").fadeTo(300, .6);
});
$("div#basketHelpLightbox").fadeTo(300, .6);
$("div#skipTab").click(function(){
$("#basketHelpLightbox, #basketHelpLightbox-panel").fadeOut(300);
});
});
jQuery("div#basketHelpLightboxPopUp").show();
}
}

function closeBasketHelpLightbox(returnToCartLightbox, returnToWishlistLightbox){
jQuery("div#basketHelpLightboxPopUp").hide();
$("#basketHelpLightbox, #basketHelpLightbox-panel").fadeOut(300);
if(returnToCartLightbox){
showCartLightbox();
} else if (returnToWishlistLightbox) {
showWishlistLightbox();
}
}
// lots more functions

我确实尝试将 export default {showBasketHelpLightbox, closeBasketHelpLightbox} 添加到 lightbox.js 的底部,然后将这些函数名称作为混合导入到 Vue 组件中。但它提示该文件中的 default 词。

最佳答案

您可以通过 computed-prop 访问 lightbox: https://jsfiddle.net/L3fjdwuh/

// lodash.js example
new Vue({
el: '#app',
data: {
arr: [1,2,3,4],
},
computed: {
LODASH() {
return _;
},
},
});

UPD(通过评论):

// in your legacy/static/js/lightbox
export default {
showBasketHelpLightbox,
};


// in your component
import { default as lightbox } from '...';

...
computed: {
lightbox() {
return lightbox;
},
},
...

关于javascript - Vue.js - 如何访问外部 Javascript/Jquery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54538748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com