简体   繁体   English

将外部脚本添加到 vue.js

[英]Add External script into vue.js

I would like to include into my vue.js application a simple javascript script with an external link to the library, this one:我想在我的 vue.js 应用程序中包含一个简单的 javascript 脚本,其中包含一个指向库的外部链接,这个:

<script type="text/javascript" src="https://my_site/index.js"></script>
 <link rel="stylesheet" href="https://my_site/index.css" />

 <script type="text/javascript">
     document.addEventListener('DOMContentLoaded', function () {
         new Pbx(
             {
                 "key1": 'value1',
                 "key2": 'value2'
             },
             {
                 'config': 'configValue'
             }
         );
     });
 </script>

I have this main.js file and I would like to keep external my script:我有这个 main.js 文件,我想将我的脚本保留在外部:

    import Vue from 'vue';
    import VModal from 'vue-js-modal';
    import App from './App.vue';
    import router from './router';
    import store from './store';
    import i18n from './i18n';
    import VueScrollactive from 'vue-scrollactive';
    import VTooltip from 'v-tooltip';

    import './styles/index.scss';

    export default new Vue({
      el: '#myApp',
      store,
      router,
      i18n,
      render: h => h(App),
    });

I would like to know how to include my library in a good way, I have tried to include my javascrupt here but I don't like it so much.我想知道如何以一种好的方式包含我的库,我试图在这里包含我的 javascrupt 但我不太喜欢它。

Thanks谢谢

I remember I had used jQuery without installing it in npm, as long as you place <script> tag of jQuery on top of script tag of the build files that is placed inside the generated html file you may able to access the $ variable of jQuery inside your app.我记得我使用过 jQuery 而没有在 npm 中安装它,只要您将 jQuery 的<script>标签放在生成的 html 文件中的构建文件的script标签之上,您就可以访问 jQuery 的$变量在您的应用程序中。

You may do something like creating a separate .js file for that variable you want to access.您可以执行一些操作,例如为要访问的变量创建单独的.js文件。 let pbx = new Pbx(data) and the pbx data is accessible throughout the app as long the placing of script tags is proper. let pbx = new Pbx(data)并且pbx数据可以在整个应用程序中访问,只要脚本标签的放置是正确的。

One thing though, document.addEventListener('DOMContentLoaded') might interfere with vue, however you can access the pbx variable to the mounted life cycle of App component if you want to access it immediately不过有一件事, document.addEventListener('DOMContentLoaded')可能会干扰 vue,但是如果你想立即访问它,你可以访问pbx变量到App组件的mounted生命周期

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

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