How to integrate mailchimp with nuxt js app?

半城伤御伤魂 提交于 2019-12-12 04:46:48

问题


I'd like to embed the following script into my component in nuxtjs app. But since nuxt has no solution for this. I'd like to ask the vue community to see if there was a better way of embedding external 3rd party js scripts?

Where should I be embedding this type of scipt? And what sort of external configurations do I need to add to enable this to work?

I tried adding directly into my template but it doesn't seem to be working.

<script type="text/javascript" src="//downloads.mailchimp.com/js/signup-forms/popup/embed.js" data-dojo-config="usePlainJson: true, isDebug: false"></script><script type="text/javascript">require(["mojo/signup-forms/Loader"], function(L) { L.start({"baseUrl":"mc.us17.list-manage.com","uuid":"XXXXXX","lid":"XXXXXX"}) })</script>

Thanks!


回答1:


Open up your nuxt.config.js file, and search for the head object. You can add your third party scripts there like this:

// nuxt.config.js
module.exports = {
  head: {
    title: 'My title',
    // etc.
    script: [
      { src: "//downloads.mailchimp.com/js/signup-forms/popup/embed.js" },
      // You can add multiple third-party scripts here
    ]
  },
  // Other stuff
}

Docs: How to use external resources?

I haven't tested it, but I think, since the other part is just javascript, you can add into your page you wan't to show it or a layout file (if you want to show it on multiple pages), like this:

// layout/default.vue
<template>
  <!-- Your template -->
</template>

<script>
  require(["mojo/signup-forms/Loader"], function(L) { L.start({"baseUrl":"mc.us17.list-manage.com","uuid":"XXXXXX","lid":"XXXXXX"}) });
  export default {
    // etc...
  }
</script>

Although the require part might mess things up...

Let me know if this works!




回答2:


Insert the MailChimp code inside the <head>

head() {
    return {
      script: [
        {
          type: 'text/javascript',
          src:
            '//downloads.mailchimp.com/js/signup-forms/popup/unique-methods/embed.js',
          'data-dojo-config': 'usePlainJson: true, isDebug: false'
        },
        {
          type: 'text/javascript',
          innerHTML: `window.dojoRequire(['mojo/signup-forms/Loader'], function(L) {
                          L.start({
                            baseUrl: 'mc.us17.list-manage.com',
                            uuid: 'your_uuid',
                            lid: 'your_lid',
                            uniqueMethods: true
                          })
                        })`
        }
      ],
      __dangerouslyDisableSanitizers: ['script']
    }
  }


来源:https://stackoverflow.com/questions/46818889/how-to-integrate-mailchimp-with-nuxt-js-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!