vue ssr如何在脚本标签中添加属性

时间:2020-08-21 05:41:47

标签: javascript vue.js

我将文档guid与https://ssr.vuejs.org/一起使用,但是找不到任何在script标记上添加自定义属性的方法。我想在script标记上添加 double randomArray[sizeOfRandomArray] {1,2,3}; 以获得正确的错误, “脚本错误”。

1 个答案:

答案 0 :(得分:0)

我找到了一种为Scaript标签添加自定义属性的方法, 只需使用·Manual Asset Injection·,然后您将获得脚本字符串,然后对其进行更改。 该文档在这里:https://ssr.vuejs.org/zh/guide/build-config.html#%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%85%8D% E7%BD%AE-client-config

代码打击:

    const content = await bundleRenderer.renderToString(context);
    console.log('context.renderResourceHints()',context.renderResourceHints())
    console.log('context.renderStyles()',context.renderStyles())
    console.log('context.renderScripts()',context.renderScripts())

    const html = `
<!DOCTYPE html>
<html>
  <head>
    ${context.renderResourceHints()}
    ${context.renderStyles()}
    <title>Hello Vue 13</title>
  </head>
  <body>
    <div id="app">${content}</div>
    ${context.renderScripts()}
  </body>
</html>  
    `.trim();

    res.send(html);

结果是:

context.renderResourceHints() <link rel="preload" href="/dist/manifest.js" as="script"><link rel="preload" href="/dist/css/styles.css" as="style"><link rel="preload" href="/dist/js/styles.5d70cd50ea7f1b9c411b.js" as="script"><link rel="preload" href="/dist/js/vendors~app.4be2234d42fb0ae60615.js" as="script"><link rel="preload" href="/dist/js/app.5db4c221de97601de33e.js" as="script"><link rel="prefetch" href="/dist/js/about.8e31d4893b2bd4ba6eee.js"><link rel="prefetch" href="/dist/js/blank.d47479964602e2a1ba7b.js">
context.renderStyles() <link rel="stylesheet" href="/dist/css/styles.css">
context.renderScripts() <script src="/dist/manifest.js" defer></script><script src="/dist/js/styles.5d70cd50ea7f1b9c411b.js" defer></script><script src="/dist/js/vendors~app.4be2234d42fb0ae60615.js" defer></script><script src="/dist/js/app.5db4c221de97601de33e.js" defer></script>

我们得到了可以正确更改的字符串?