轻松地将脚本标签添加到 nuxtjs

Smo*_*son 4 vue.js vuejs2 nuxt.js

基本上我需要将脚本添加到我的 index.html 的头部,

<script type="text/javascript" src="https://a.optmnstr.com/app/js/api.min.js" data-account="XXXXX" data-user="XXXXX" async></script>
Run Code Online (Sandbox Code Playgroud)

所以我试过的是......

在我的 nuxt.config.js 中

head: {
    script: [
        {  
           type: 'text/javascript', 
           src: 'https://a.optmnstr.com/app/js/api.min.js',
           data-account: 'XXXXX',
           data-user: 'XXXXX',
           async: true
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

现在显然这不起作用,因为data-account并且data-user无效,所以我怎样才能使它起作用?

任何帮助,将不胜感激!

谢谢

Smo*_*son 14

有人指出这是@Andrew1325

In nuxt.js you can create a app.html file and add scripts to it, so in my case the app.html file looks like this

<!DOCTYPE html>
<html {{ HTML_ATTRS }}>
  <head>
    {{ HEAD }}
  </head>
  <body {{ BODY_ATTRS }}>
    {{ APP }}
  </body>
  <script type="text/javascript" src="https://a.optmnstr.com/app/js/api.min.js" data-account="XXXX" data-user="XXXX" async></script>
</html>
Run Code Online (Sandbox Code Playgroud)

IMO this is a very easy way to include difficult scripts into your project

NOTE

You will need to restart your project to see the app.html changes

For more information you can go nuxtjs - app template


skp*_*aik 6

这可以帮助你

export default {
  data () {
    return {
      message: '',
      head: {
        type: Object,
        default: function () {
          return {
            title: ' Default Home page  ',
            meta: [
              {
                'hid': 'description',
                'name': ' description',
                'content': ' Home page  content '
              }
            ],
            script: [
              {
                innerHTML: {
                  'url': 'https://www.example.com',
                  'logo': 'https://www.example.com/icon/logo.png',
                  'parentOrganization': {
                    'name': 'The X Company Inc',
                    'url': 'https://example.io',
                    'logo': 'https://example.io/logo-est.png',
                    '@type': 'Organization'
                  },
                  'foundingLocation': {
                    'address': {
                      'addressLocality': 'Dakar',
                      'addressRegion': 'Selegal',
                      '@type': 'PostalAddress'
                    },
                    '@type': 'Place'
                  },
                  'sameAs': ['https://www.facebook.com/example', 'https://www.twitter.com/example'],
                  '@context': 'http://schema.org',
                  '@type': 'Organization'
                },
                type: 'application/ld+json'
              }
            ]
          }
        }
      }
    }
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)


Jef*_* S. 5

您也可以将数据属性用单引号括起来,如下所示:

head: {
    script: [
        {  
            type: 'text/javascript', 
            src: 'https://a.optmnstr.com/app/js/api.min.js',
            'data-account': 'XXXXX',
            'data-user': 'XXXXX',
            async: true
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)