【发布时间】:2021-07-22 21:08:15
【问题描述】:
我必须在我的 vue Web 应用程序中嵌入一些脚本标签。我不确定在哪里添加它,但我选择将它们添加到index.html。 #app div 所在的位置。
在index.html 下方,我必须嵌入至少 3 或 4 个脚本标签,并创建一些更改语言的 cookie,设置用户的偏好,以便跟踪或不跟踪他们。为此,我需要访问 vuex 商店。
如果我尝试访问store 形式的index.html 的属性,例如this.$store.getters.getActiveLanguage;,则会出错。
Uncaught TypeError: Cannot read property 'getters' of undefined
听起来我无法从这里访问商店。我该如何解决,有人可以帮忙吗?提前谢谢你
编辑:index.html 中的完整代码示例
<!-- Cookie Consent by https://www.FreePrivacyPolicy.com -->
<script type="text/javascript" src="//www.freeprivacypolicy.com/public/cookie-consent/3.1.0/cookie-consent.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
cookieconsent.run({ notice_banner_type: "interstitial", consent_type: "express", palette: "dark", language: "en" });
});
</script>
<!-- easy -->
<!-- const language = .localSorage.getItem("language"); -->
<script type="text/plain" cookie-consent="functionality">
document.cookie = "functionalityCookie=en";
this.$store.getters.getActiveLanguage;
</script>
<script type="text/plain" cookie-consent="tracking">
document.cookie = "trackingCookie=true";
</script>
<script type="text/plain" cookie-consent="strictly-necessary">
document.cookie = "strictlyNecessaryCookie=true";
</script>
<!-- end of easy-->
<noscript
>Cookie Consent by <a href="https://www.FreePrivacyPolicy.com/free-cookie-consent/" rel="nofollow noopener">FreePrivacyPolicy.com</a></noscript
>
【问题讨论】:
-
如果没有更多示例代码,很难说问题出在哪里。但是
this.$store只能在 Vue 内部工作,例如在组件内部。在其他任何地方它都不会简单地工作。 -
我刚刚添加了完整的代码,我还指出了我想如何访问商店,但听起来我无法访问它。您认为最好的解决方案是什么?
标签: javascript cookies vuex vuejs3