0

所以我试图做他们在这里做的事情MJML - 模板插值,动态数据,上下文但是当我运行时node fun.js我得到以下错误。

file:///Users/admin/mjml/mjml/fun.mjs:2
import { compile } from 'handlebars'
         ^^^^^^^
SyntaxError: The requested module 'handlebars' does not provide an export named 'compile'

另外作为旁注,除了上面的链接之外,还有其他使用 MJML 模板的方法

我的代码

import { mjml2html } from 'mjml'
import { compile } from 'handlebars'

/*
  Compile an mjml string
*/

const template = 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)
4

1 回答 1

0

尝试这个

import { mjml2html } from 'mjml'
const Handlebars = require("handlebars");
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)

你的nodej js版本也是旧的,当前是15.0.1,这是你更新到当前的方式:

1-npm cache clean -f 
2-npm install -g n
3-npm install n latest

参考文档https://handlebarsjs.com/installation/#npm-or-yarn-recommended

于 2020-10-28T19:47:15.303 回答