简体   繁体   English

如何在 index.html 的外部脚本中添加额外的动态参数? (Vue.js)

[英]How to add additional dynamic params to the external script in index.html? (Vue.js)

I have an external script in Vuejs inside index.html我在 index.html 的 Vuejs 中有一个外部脚本

<script>window.appSettings={app_id:"appId"};</script>

I need to pass username and userEmail from getters/user like this:我需要像这样从getters/user传递usernameuserEmail

<script>window.appSettings={app_id:"appId", contact_name: "Alexander the Great",
        contact_email: "alex@empire.com",};</script>

How can this be achieved correctly?如何正确实现这一目标?

Timing will be an issue.时间将是一个问题。 Inside Vue you can write to window.appSettings at any time.在 Vue 中,您可以随时写入 window.appSettings。 I'd suggest you do this inside a created or mounted function.我建议您在创建或安装的 function 中执行此操作。

an example:一个例子:

{
   mounted() {
      window.appSettings = window.appSettings || {};
      window.appSettings.userId = this.userId;
   }
}

Because an external script will use these values it's important that Vue writes these values BEFORE the external script is loaded.因为外部脚本将使用这些值,所以 Vue 在加载外部脚本之前写入这些值很重要。

To ensure Vue get's to write first and then the script is loaded, it is best to make Vue responsible for loading the external script.为了保证 Vue 先写后加载脚本,最好让 Vue 负责加载外部脚本。 You could do this with by dynamically creating and script element and adding it to the dom, like so:您可以通过动态创建和脚本元素并将其添加到 dom 来做到这一点,如下所示:

{
   mounted() {
      // write the values first
      window.appSettings = window.appSettings || {};
      window.appSettings.userId = this.userId;

      // now load any external scripts:
      var script = document.createElement('script');
      script.src = 'hellozest.io/widget/...';
      document.body.appendChild(script);
   }
}

This strategy may work but as you can see Vue needs to know about external scripts and is responsible for loading them.这种策略可能有效,但正如您所见,Vue 需要了解外部脚本并负责加载它们。 In general I'd prefer to solve this in a different way:一般来说,我更愿意以不同的方式解决这个问题:

Is userId and userEmail available to you on the backend side of your application?您的应用程序后端是否可以使用 userId 和 userEmail? Then you could write the 'window.appSettings` on the server and let the Vue application be as simple as it should.然后,您可以在服务器上编写“window.appSettings”,让 Vue 应用程序尽可能简单。

If only Vue knows about userId and userEmail you could decide to isolate this responsibility inside one vue component, because it is important to keep this code in one place.如果只有 Vue 知道 userId 和 userEmail,你可以决定将这个职责隔离在一个 Vue 组件中,因为将这段代码保存在一个地方很重要。

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

相关问题 Vue.js css 和 index.html 中声明的 js 不起作用 - Vue.js css and js declared in index.html are not working Vue.js应用程序上index.html中的脚本包含给出404 - Script inclusion in index.html on Vue.js application gives 404 如何将 index.html/index.jsp 上定义的 Javascript 变量用于 Vue.js? - how to make use of Javascript variable which are defined on index.html/index.jsp to Vue.js? 如何在React样板中的index.html上添加外部JS? - How to add external JS on index.html in React boilerplate? Vue.js 构建器在索引中生成不正确的路径。html - Vue.js builder produces incorrect paths in index.html 将外部脚本添加到 vue.js - Add External script into vue.js 如何从 index.html 向 Vue.js 3 实例发送消息? - How to send a message from index.html to Vue.js 3 instance? Vue.js / Webpack 使用 CSS 和 JS 的“静态”子域构建“index.html” - Vue.js / Webpack build “index.html” using a “static” subdomain for CSS and JS 如何通过自动将NPM“依赖”包含到index.html中<script> tags in VUE JS WEBPACK project? - How to automatically include NPM “dependencies” into index.html through <script> tags in VUE JS WEBPACK project? 如何在不触及index.html的情况下将外部javascript标记添加到Ember JS应用程序? - How can I add an external javascript tag to an Ember JS app without touching the index.html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM