javascript - 如何从 vuex 商店将参数传递给 vue 中的 IIFE?
问题描述
我们有两个脚本在启动时加载到 head 标签中。其中一个脚本指向本地 .js 文件中的 IIFE。这是第三方软件。
/*
Inline configuration
*********************
Object is now being instantiated against the OOo object (1 global class)
To call this object, place the below in the click event
OOo.oo_launch(event, 'oo_feedback1')
*/
(function (w, o) {
'use strict'
var OpinionLabInit = function () {
// for SPA deployment, o.inlineFeedShow would be the function we tell clients to add to the onclick event of their inline link
o.inlineFeedbackShow = function (event) {
var replacePattern = '://' + window.location.host
if (window.location.host !== 'src.companyX.com' && window.location.host !== 'checkout.companyX.com') {
replacePattern = '://test.checkout.companyX.com'
}
o.oo_feedback = new o.Ocode({
referrerRewrite: {
searchPattern: /:\/\/[^/]*/,
replacePattern: replacePattern
},
customVariables: {
flow: '',
clientId: '',
srcCorrelationId: '',
visitorId: '',
sAccount: ''
}
})
// Now that oo_feedback has been re-initialized with the custom variable and contextual of the current page, launch the comment card
o.oo_launch(event, 'oo_feedback')
}
o.oo_launch = function (e, feedback) {
var evt = e || window.event
o[feedback].show(evt)
}
if (typeof OOo !== 'undefined' && typeof OOo.releaseDetails !== 'object') {
OOo.releaseDetails = []
}
OOo.releaseDetails.push({
author: 'DN',
timeStamp: '07/26/2019, 10:56:32',
fileName: 'oo_conf_inline.js',
fileVersion: '1.0',
ticketNumber: 'DYN-1042506',
gitDiff: 'N/A'
})
};
OpinionLabInit()
})(window, OOo)
我想要做的是从 Vuex 存储中传递一些状态对象属性作为“customVariables”对象中的值。
例如:我想使用 from store/modules/user.js
state = {
flow.NEW_USER
}
并在 customVariables 对象中
customVariables: {
flow: flow.NEW_USER,
clientId: '',
srcCorrelationId: '',
visitorId: '',
sAccount: ''
}
解决方案
检查state
变量是否在所有相邻.js
文件中全局可用。如果是这样,您可以修改IIFE以注入第三个参数,这将是您的state
变量。
我认为你的state
变量对象符号应该是这样的。
state = {
flow: flow.NEW_USER
}
给IIFE第三个参数称为s
(或任何你喜欢的)。
(function (w, o, s) {
'use strict'
...
o.oo_feedback = new o.Ocode({
referrerRewrite: {
searchPattern: /:\/\/[^/]*/,
replacePattern: replacePattern
},
customVariables: {
flow: s.flow, // flow.NEW_USER is now here
clientId: '',
srcCorrelationId: '',
visitorId: '',
sAccount: ''
}
})
...
})(window, OOo, state)
state
并在底部 注入变量。
现在,这仅在您对state
变量具有全局访问权限时才有效。它必须在IIFE执行之前定义。
推荐阅读
- python-3.x - Windows 上的 Python 子进程:启动子进程“cmd.exe”并为其提供 bat 文件,停止主进程执行
- mysql - 如何从具有不同ID的表中选择相同的列
- python - django-rest-framework:在 ModelViewSet 中添加批量操作
- java - 如何使用 vavr 返回 void 或 String
- sql-server - 将 XML 数据插入到表中
- rbokeh - rbokeh 颜色参数 - 传说在 R 3.6 上搞砸了
- sql - 创建表数据库
- tensorflow - 数值稳定的张量流
- c# - Entity Framework Core - 如何提取有关底层 SQL 语法的详细信息?
- python - 无法将 csv 文件从烧瓶上传到数据库