美文网首页
去除nuxt中window.__NUXT__=

去除nuxt中window.__NUXT__=

作者: 泪滴在琴上 | 来源:发表于2023-09-09 23:05 被阅读0次

nuxt.config.js中,加入这段代码

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

因为我的首页加载的数据,是在async asyncData里面加载的,然加入这段代码,我服务端加载数据,就不在源代码了,看不到了,然后我就使用了另外一种方法

//安装cheerio
npm install cheerio
 
//然后在nuxt.confid.js代码中使用
const cheerio = require('cheerio');
module.exports = {
 hooks: {
    'render:route': (url, result) => {
      //  window.__nuxt__位于body中的第一个script中 移除了body中第一个脚本标签
      this.$ = cheerio.load(result.html, { decodeEntities: false })
      this.$(`body script`).eq(0).remove()
      result.html = this.$.html()
    },
 }
}

再查看源代码的时候,服务端请求的数据也加载出来了,然后window.NUXT=(function(a...))这段代码也没有了

相关文章

网友评论

      本文标题:去除nuxt中window.__NUXT__=

      本文链接:https://www.haomeiwen.com/subject/xxajvdtx.html