在Nuxt.js中使用Vue-Meta内联Js。

问题描述 投票:2回答:1

基本上,我使用Nuxt 2.9.2,并试图使用innerHTML方法来内联一个Google Optimize脚本,但每当我运行npm运行生成,代码会改变某些方面,即使__dangerouslyDisableSanitizers是白名单innerHTML。

这是我在Nuxt配置头对象中的脚本。

script: [
    {
      innerHTML: `(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;})(window,document.documentElement,'async-hide','dataLayer', 500 , ${JSON.stringify(
        { [process.env.GOOGLE_OPTIMIZE_ID]: true }
      )})`
    }
  ],
  __dangerouslyDisableSanitizers: ['innerHTML']
},

渲染出如下图,尝试了多种不同的方式,无法让它如期内嵌。

  !function(e,n,t,a,c,s,d){n.className+=" "+t,s.start=1*new Date,s.end=d=function(){n.className=n.className.replace(RegExp(" ?"+t),"")},(e[a]=e[a]||[]).hide=s,setTimeout(function(){d(),s.end=null},500),s.timeout=500}(window,document.documentElement,"async-hide","dataLayer",0,{"GTM-XXXXXX":!0})

应是

(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;})(window,document.documentElement,'async-hide','dataLayer', 500 , 'GTM-XXXXXX'': true }
        )})
javascript vue.js nuxt.js nuxt vue-meta
1个回答
0
投票
        script: [
          {
            innerHTML: `window.MY_CONST = 'abcd1234'`,
            type: 'text/javascript',
            charset: 'utf-8',
          },
        ],
        __dangerouslyDisableSanitizers: ['script', 'innerHTML'],
© www.soinside.com 2019 - 2024. All rights reserved.