首页 > 解决方案 > 如何从 Nuxt 中的 Vuex 中删除 window.__NUXT__ 而不会出现任何问题

问题描述

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

这是情况:lang.json我的应用程序中有一个文件。我阅读它并将其存储langVuex. 但window.__Nuxt__显示我不想的语言!

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

1:通过将此代码添加到 nuxt.config.js 链接到堆栈答案

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 文章链接中的一些代码

3:通过使用cheerio包并从正文 链接到文章中抓取脚本

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

它会给出错误 webpack 不能延迟加载 this 为this.$store.state.siteDirection空。

我该如何解决这个问题?

标签: vuejs2nuxt.jsvuexvue-render-function

解决方案


推荐阅读