简体   繁体   English

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

[英]Vue.js - how to access external Javascript/Jquery functions

I am working on creating a lightbox/modal component in Vue.js. 我正在努力在Vue.js中创建一个灯箱/模态组件。 I've got it mostly working and now I want to use some existing functions from another file that are written in Javascript/Jquery. 我已经得到它主要工作,现在我想使用另一个用Javascript / Jquery编写的文件中的一些现有函数。 It's too big a task to rewrite these in Vue at this point, so we just need to use them as is for now. 这时在Vue中重写这些是太大的任务,所以我们现在只需要按原样使用它们。 But I'm hung up on how to get access to them. 但我已经挂断了如何访问它们。 I'm not sure if I need to import it like a mixin or something different. 我不确定我是否需要像mixin或其他东西那样导入它。

As the code is now, I am getting the error: [Vue warn]: Property or method "lightbox" is not defined on the instance but referenced during render. 由于代码现在,我收到错误: [Vue warn]: Property or method "lightbox" is not defined on the instance but referenced during render.

Here is the relevant code from my Vue component: 以下是我的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>

And the relevant code from lightbox.js 以及来自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

I did try adding export default {showBasketHelpLightbox, closeBasketHelpLightbox} to the bottom of lightbox.js and then just importing those function names as mixins in the Vue component. 我尝试将export default {showBasketHelpLightbox, closeBasketHelpLightbox}到lightbox.js的底部,然后将这些函数名称作为mixin导入到Vue组件中。 But it complained about the default word in that file. 但它抱怨该文件中的default单词。

You can access to lightbox by computed-prop: https://jsfiddle.net/L3fjdwuh/ 您可以通过computed-prop访问lightbox: https//jsfiddle.net/L3fjdwuh/

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

UPD (by comments): UPD(评论):

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


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

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

You should be able to just add it to the data properties: 您应该只需将其添加到数据属性:

import lightbox from '../../../legacy/static/js/lightbox';

export default {
  name: "AddToCartModal",
  components: {.....},
  data: () => ({
    lightbox,
  }),
}

You shouldn't include it in mixins unless it is an actual vue mixin. 你不应该把它包含在mixins中,除非它是一个真正的vue mixin。 Adding it to the data props should allow you to reference it within the template. 将其添加到数据道具应该允许您在模板中引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM