【发布时间】:2019-03-07 02:08:04
【问题描述】:
如何将样式异步加载到网站。还是在页脚中插入样式?
我使用 nuxt: 2.0.0
我试试:
- 在 webpack 中添加插件:async-stylesheet-webpack-plugin。但是,在 nuxt 中预渲染添加样式。
- 创建模块。但是如何删除旧样式?
谢谢!
【问题讨论】:
标签: javascript vue.js nuxt.js
如何将样式异步加载到网站。还是在页脚中插入样式?
我使用 nuxt: 2.0.0
我试试:
谢谢!
【问题讨论】:
标签: javascript vue.js nuxt.js
我开发模块:
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)
}
【讨论】: