Node.js/nuxt/其他:修订间差异
来自康健生活
| 第38行: | 第38行: | ||
const buffer= fs.readFileSync('./assets/fonts/icon/iconfont.js') | const buffer= fs.readFileSync('./assets/fonts/icon/iconfont.js') | ||
module.exports = `${String(buffer)}`</syntaxhighlight>}} | 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> | |||
2020年9月4日 (五) 04:12的最新版本
页面组件加载 js
vue-meta
head: {
script: [{ innerHTML: require('./assets/js/flexible'), type: 'text/javascript', charset: 'utf-8'}],
// 不对<script>标签中内容做转义处理
__dangerouslyDisableSanitizers: ['script']
}
head: {
script: [{ src: 'http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js' }],
__dangerouslyDisableSanitizers: ['script']
},
在 nuxt.config.js 中引用会抛错
需要对引用的代码进行修改
// 待引入的 js 文件
module.exports = `if (window) { js文件内容... }`
或者
(function(window) {
/* Keep source code the same */
// })(typeof window == "undefined" ? global : window);
// or
})(this);
在这里我添加了一个中转文件,这样就不用每次修改 iconfont 都要修改 iconfont.js
plugins/icon.ts
/**
* @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)}`
添加 css 文件
有个新需求是也许还会添加 css 文件
nuxt.config.js
// 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()},
],
同时我把 icon.ts 文件更新了一下
/**
* @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)}`
}
No categories