【问题标题】:Nuxt 3 Local JS InclusionNuxt 3 本地 JS 包含
【发布时间】:2023-02-16 18:01:59
【问题描述】:
与 Nuxt 3 撞墙并包含本地 JS 文件。我将一个 HTML 模板转换为一个主布局和几个组件。还添加在主题提供的 CSS 中作为全局。最后一部分是包含主题提供的以下本地 JS 文件:
custom.js, bootstrap.min.js, and slider.js
我尝试将它们作为插件添加到 head 元素中的 nuxt.config.ts 中,也尝试添加到布局组件中,但这些方法似乎都不起作用。我似乎无法为 Nuxt 3 找到一个干净的工作答案。
【问题讨论】:
标签:
javascript
nuxt.js
nuxtjs3
【解决方案1】:
在 nuxt3 中,您可以从组件添加全局资产。
useHead({
script: [{
async: true,
src: 'https://accounts.google.com/gsi/client',
defer: true
}]
})
例如从你的主要布局。
如果您的脚本需要更多道具,请查看此界面:
interface ScriptBase {
/**
* For classic scripts, if the async attribute is present,
* then the classic script will be fetched in parallel to parsing and evaluated as soon as it is available.
*
* For module scripts,
* if the async attribute is present then the scripts and all their dependencies will be executed in the defer queue,
* therefore they will get fetched in parallel to parsing and evaluated as soon as they are available.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-async
*/
async?: Booleanable;
/**
* Normal script elements pass minimal information to the window.onerror
* for scripts which do not pass the standard CORS checks.
* To allow error logging for sites which use a separate domain for static media, use this attribute.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-crossorigin
*/
crossorigin?: '' | 'anonymous' | 'use-credentials';
/**
* This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document
* has been parsed, but before firing DOMContentLoaded.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-defer
*/
defer?: Booleanable;
/**
* Provides a hint of the relative priority to use when fetching an external script.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-fetchpriority
*/
fetchpriority?: 'high' | 'low' | 'auto';
/**
* This attribute contains inline metadata that a user agent can use to verify
* that a fetched resource has been delivered free of unexpected manipulation.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-integrity
*/
integrity?: string;
/**
* This Boolean attribute is set to indicate that the script should not be executed in browsers
* that support ES modules — in effect,
* this can be used to serve fallback scripts to older browsers that do not support modular JavaScript code.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-nomodule
*/
nomodule?: Booleanable;
/**
* A cryptographic nonce (number used once) to allow scripts in a script-src Content-Security-Policy.
* The server must generate a unique nonce value each time it transmits a policy.
* It is critical to provide a nonce that cannot be guessed as bypassing a resource's policy is otherwise trivial.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-nonce
*/
nonce?: string;
/**
* Indicates which referrer to send when fetching the script, or resources fetched by the script.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-referrerpolicy
*/
referrerpolicy?: ReferrerPolicy;
/**
* This attribute specifies the URI of an external script;
* this can be used as an alternative to embedding a script directly within a document.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-src
*/
src?: string;
/**
* This attribute indicates the type of script represented.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-type
*/
type?: '' | 'text/javascript' | 'module' | 'application/json' | 'application/ld+json' | 'speculationrules' | (string & Record<never, never>);
/**
* This attribute defines the unique ID.
*/
id?: string;
}
type Script = ScriptBase & HttpEventAttributes;
您可以为文件使用 /public 目录,并使用相关补丁附加它们。