Node.js/nuxt/其他:修订间差异
来自康健生活
| (未显示同一用户的4个中间版本) | |||
| 第4行: | 第4行: | ||
head: { | head: { | ||
script: [{ innerHTML: require('./assets/js/flexible'), type: 'text/javascript', charset: 'utf-8'}], | script: [{ innerHTML: require('./assets/js/flexible'), type: 'text/javascript', charset: 'utf-8'}], | ||
// 不对<script>标签中内容做转义处理 | |||
__dangerouslyDisableSanitizers: ['script'] | __dangerouslyDisableSanitizers: ['script'] | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
| 第13行: | 第14行: | ||
在 nuxt.config.js 中引用会抛错 | 在 nuxt.config.js 中引用会抛错 | ||
{{Warning|ReferenceError: window is not defined}} | {{Warning|ReferenceError: window is not defined}} | ||
需要对引用的代码进行修改 | |||
<syntaxhighlight lang="javascript"> | |||
// 待引入的 js 文件 | |||
module.exports = `if (window) { js文件内容... }`</syntaxhighlight> | |||
或者 | |||
<syntaxhighlight lang="javascript"> | |||
(function(window) { | |||
/* Keep source code the same */ | |||
// })(typeof window == "undefined" ? global : window); | |||
// or | |||
})(this);</syntaxhighlight> | |||
在这里我添加了一个中转文件,这样就不用每次修改 iconfont 都要修改 iconfont.js | |||
{{hc|plugins/icon.ts|<syntaxhighlight lang="javascript"> | |||
/** | |||
* @author kangkk | |||
* @description 引用 iconfont 里面的 js 文件,直接引用会报出 window 未定义 | |||
* @type {module:fs} | |||
*/ | |||
const fs=require("fs") | |||
const buffer= fs.readFileSync('./assets/fonts/icon/iconfont.js') | |||
module.exports = `${String(buffer)}`</syntaxhighlight>}} | |||
=== 添加 css 文件 === | |||
有个新需求是也许还会添加 css 文件 | |||
{{hc|nuxt.config.js|<syntaxhighlight lang="javascript"> | |||
// html head 中创建 script 标签 | |||
script: [ | |||
{innerHTML: require('./plugins/icon.ts').iconJs(), type: 'text/javascript', charset: 'utf-8'} | |||
], | |||
// 不对<script>标签中内容做转义处理 | |||
__dangerouslyDisableSanitizers: ['script','style'], | |||
link: [ | |||
{rel: 'icon', type: 'image/x-icon', href: '/favicon.ico'}, | |||
], | |||
style: [ | |||
{rel: 'iconfont', type: 'text/css', innerHTML: require('./plugins/icon.ts').iconCss()}, | |||
],</syntaxhighlight>}} | |||
同时我把 icon.ts 文件更新了一下 | |||
<syntaxhighlight lang="javascript"> | |||
/** | |||
* @author kangkk | |||
* @description 引用 iconfont 里面的 js 文件,直接引用会报出 window 未定义 | |||
* @type {module:fs} | |||
*/ | |||
const fs = require("fs") | |||
// module.exports = `${String(buffer)}` | |||
export function iconJs () { | |||
const buffer = fs.readFileSync('./assets/fonts/icon/iconfont.js') | |||
return `${String(buffer)}` | |||
} | |||
export function iconCss () { | |||
const buffer = fs.readFileSync('./assets/fonts/icon/iconfont.css') | |||
return `${String(buffer)}` | |||
}</syntaxhighlight> | |||