javascript - 如何在我的代码 nuxtjs 上导入外部 js 和 css

标签 javascript nuxt.js

我在 Nuxt.JS 中启动了一个项目,但已经有了 HTML + CSS + JS 的网站。我只需要在 NuxtJS 的设计中包含这些 HTML 即可。

我已经尝试将 css 作为全局范围放入 nuxt.config.js 中,但是这样它将在 SSR 中编译,我不喜欢它。就像在 html 上导入 css、js...

如何将 CSS 和 Javascript 导入到我的页面中?

谢谢

最佳答案

您所要做的就是在页面的默认导出中包含一个 head 方法。
可以详细了解head方法here

<script>
export default {
  head () {
    return {
      script: [
        { src: 'path/to/your/js/file.js' }
      ],
      link: [
        { rel: 'stylesheet', href: 'path/to/your/css/file.css' }
      ]
    }
  }
}
</script>

您可以像在 html 中使用 style 标记一样包含它们

<style src="path/to/your/css/file.css"><style>
<!-- You can also add "scoped" or "lang='sass'" etc in there -->

关于javascript - 如何在我的代码 nuxtjs 上导入外部 js 和 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53874997/

相关文章:

javascript - 向 d3 SVG map 添加点

vue.js - NuxtJS - 无法读取未定义的属性 'title' - 奇怪的行为

javascript - 在多维数组中查找 int 并在 Angular JS 中应用类

javascript - 使用哪种方法来访问页面 URL?

nuxt.js - 如何使用 vue-i18n 更改属性 lang html

javascript - Nuxt js自定义光标事件监听器在路由更改后不起作用

javascript - 无法将 npm 包添加到 nuxt js [vue-star- rating]

javascript - 如何将eslint配置正确添加到NuxtJS?

Javascript Promise 未定义

javascript - 属性未定义