0

所以我生成了一些 HTML,我有一个包含名为 html 的 HTML 的对象,我想将它写入一个新文件,但现在它不起作用,因为我保存的文件中只有 NaN。这是我到目前为止所拥有的

import mjml2html from 'mjml'
import Handlebars from 'handlebars'
import fs from 'fs'

const template = Handlebars.compile(`
  <mjml>
  <mj-body>
    <mj-section background-color="#F0F0F0" padding-bottom="0">
      
      <mj-column  padding-left="70px" width="250px">
        
        <mj-text font-style="italic" font-size="22px" color="#626262">watFriends</mj-text>
        
      </mj-column>
      
      <mj-column width="170px"> 
            <mj-image width="30px" src={{logo}} />
        </mj-column>
    </mj-section>
    
    <mj-section background-color="#FAFAFA">
      <mj-column width="400px">
        <mj-text font-style="italic" font-size="15px" font-family="Helvetica Neue" color="#626262">
          Dear {{firstName}},
        </mj-text>
        <mj-text color="#525252">{{message}}
        </mj-text>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>
    
`)
const context = {
  firstName: '',
  message: 'hello',
  logo: 'logo.png',
}
const mjml = template(context)
const html = mjml2html(mjml)
console.log(html)

fs.writeFile('new.html', html.toString(), { encoding: 'utf8' }, function (err) {
  if (err) {
    return console.log(err)
  }
  console.log('The file was saved!')
})

4

1 回答 1

1

mjml2html返回一个对象,该对象的{html: ' ...html here..', json: {}, errors: []}toString() 是"[Object object]"您正在写入文件的字符串。改变

const html = mjml2html(mjml)

const {html} = mjml2html(mjml)

一切都会好起来的

于 2020-10-29T14:46:10.113 回答