如何在next中排除某些路由上的脚本?



我有一个nuxt2-webapp有很多路由和所有路由除了2,我需要一个脚本被注入。有办法使注射失效吗?

Nuxt配置:

meta: [
script: [
{
id: 'Cookiebot',
src: 'https://consent.cookiebot.com/uc.js',
'data-cbid': 'xxxxx',
type: 'text/javascript',
async: true,
},
],
]

不行,没有办法。
如果您注入一个脚本,它将在window对象上。因此可以在DOM的任何地方使用。

这有什么问题?你可以用一些CSS来隐藏你的东西,如果它看起来很烦人的话。

实现此行为的最佳方法是通过插件:

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);
}
};

最新更新