微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 Nuxt 中的 Vuex 中删除 window.__NUXT__ 没有任何问题

如何解决如何从 Nuxt 中的 Vuex 中删除 window.__NUXT__ 没有任何问题

我使用的是 Nuxt 2.13,但遇到了 window.__Nuxt__(function(a,b,c,d,.....)) 问题。我不知道它是否会影响我的 SEO,但它让我很紧张,并显示了我所有的语言文件

情况如下:我的应用中有一个 lang.json 文件。我阅读了它并将其存储在 lang 中的 Vuex 状态。但是 window.__Nuxt__ 显示了我不想显示的语言!!

到目前为止,我已经找到了三个解决方案来删除它:

1:通过将此代码添加nuxt.config.js link to stack answer

hooks: {
    'vue-renderer:ssr:context'(context) {
      const routePath = JSON.stringify(context.nuxt.routePath);
      context.nuxt = {serverRendered: true,routePath};
    }
  }
}

2:通过注释node_module/@nuxt/vue-renderer/dist/vue-renderer.js中的一些代码 link to article

3:通过使用cheerio包并从正文中抓取脚本 link to article

const cherrio = const cheerio = require('cheerio');
export default {
//The rest configuration is omitted
hooks: {
    'render:route': (url,result) => {
      this.$ = cheerio.load(result.html,{decodeEntities: false});
      //Since window.__nuxt__ is always located in the first script in the body,//So I removed the first script tag in the body
      this.$(`body script`).eq(0).remove();
      result.html = this.$.html()
    }
  }
}

这三个都可以完成工作,但是!!我的组件将不再被延迟加载,因为我在 Vuex 中使用状态来为延迟加载提供主题地址!例如:

computed:{
  mycomponent(){
    return ()=>import(`~/components/${this.$store.state.siteDirection}/mycomp.vue`)
  }
}

this.$store.state.siteDirection 为 null 时,webpack 无法延迟加载它会报错。

我该如何解决这个问题??

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。