【问题标题】:asynchronously load styles using nuxt使用 nuxt 异步加载样式
【发布时间】:2019-03-07 02:08:04
【问题描述】:

如何将样式异步加载到网站。还是在页脚中插入样式?

我使用 nuxt: 2.0.0

我试试:

  • 在 webpack 中添加插件:async-stylesheet-webpack-plugin。但是,在 nuxt 中预渲染添加样式。
  • 创建模块。但是如何删除旧样式?

谢谢!

【问题讨论】:

    标签: javascript vue.js nuxt.js


    【解决方案1】:

    我开发模块:

    const STYLE_REGEXP = '<link rel="stylesheet" href="(.+?)">'
    const STYLE_TEMPLATE = '<link rel="preload" href="$1" as="style" onload="this.onload=null;this.rel=\'stylesheet\'">' +
      '<noscript><link rel="stylesheet" href="$1"></noscript>'
    
    module.exports = async function asyncCss (options) {
      this.nuxt.hook('render:route', renderRouteHandle.bind(this))
    }
    
    async function renderRouteHandle (url, result) {
      result.html = result.html.replace(
        new RegExp(STYLE_REGEXP, 'g'), STYLE_TEMPLATE)
    }
    

    帮我https://github.com/nuxt/nuxt.js/issues/2822

    【讨论】:

      猜你喜欢
      • 2015-04-08
      • 2019-08-18
      • 2019-03-07
      • 2020-07-24
      • 2022-08-02
      • 2020-05-06
      • 2021-12-04
      • 2012-10-11
      • 2021-11-01
      相关资源
      最近更新 更多