我有一个nuxt2-webapp与很多路线和所有路线,除了2,我需要一个脚本注入。有什么方法可以阻止注射吗?
Nuxt配置:
meta: [
script: [
{
id: 'Cookiebot',
src: 'https://consent.cookiebot.com/uc.js',
'data-cbid': 'xxxxx',
type: 'text/javascript',
async: true,
},
],
]发布于 2022-05-06 08:43:37
不,没有办法这样做。
如果注入一个脚本,它将位于window对象上。因此,您的DOM中的任何地方都可以使用。
有什么问题吗?你可以用一些CSS隐藏你的东西,如果它是令人讨厌的视觉。
发布于 2022-05-07 18:27:06
实现这种行为的最佳方法是通过一个插件:
export default (context: Context) => {
if (!context.route.name?.includes('embed')) {
const scriptTag = document.createElement('script');
scriptTag.id = 'Cookiebot';
scriptTag.src = 'https://consent.cookiebot.com/uc.js';
scriptTag.setAttribute('data-cbid', 'xxxx');
scriptTag.type = 'text/javascript';
scriptTag.async = true;
document.head.appendChild(scriptTag);
}
};https://stackoverflow.com/questions/72138496
复制相似问题