【发布时间】:2019-06-01 16:46:32
【问题描述】:
我正在将现有的 React 应用程序集成到 Next.js 中,主要用于 SEO 功能。
我在Next.js 的<Header> 标记内粘贴了css 文件链接,它们似乎工作得很好。当我尝试使用 <script> 标记对 javascript 文件执行相同操作时,它不会执行这些脚本中的代码。但我可以在 chrome 开发工具中看到它们加载了 http 200。
我尝试使用来自npm 的名为Loadjs 的库来加载componentDidMount 中的脚本,但我得到的结果与使用<script> 标记完全相同。
在Next.js 中是否有合适的方法来做我不知道的简单事情?
这是pages/index.js 文件中包含的代码。
import React from "react"
import Head from 'next/head'
import MyAwesomeComponent from "../components/mycomponent.js"
export default () => (
<div>
<Head>
<link type="text/css" rel="stylesheet" href="static/css/chatwidget.css" />
<link type="text/css" rel="stylesheet" href="static/css/download.css" />
<script type="text/javascript" src="static/libs/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/malihu-custom-scrollbar-plugin@3.1.5/jquery.mCustomScrollbar.concat.min.js"></script>
<script type="text/javascript" src="static/libs/bootstrap.min.js"></script>
<script type="text/javascript" src="static/libs/owl.carousel.min.js"></script>
<script type="text/javascript" src="static/scripts/chatHead.js"></script>
<script type="text/javascript" src="static/libs/jquery.magnific-popup.js"></script>
</Head>
<MyAwesomeComponent /> {/* a simple React component that returns : <p>Hello World </p>*/}
</div>
)
抱歉回复晚了。
事实证明,我链接的所有scripts 都错过了一个脚本,该脚本实际上会为每个操作运行functions。
【问题讨论】:
-
您是否尝试在自定义文档中输入脚本标签。查看此页面了解如何在 nextjs 中创建自定义文档标签的更多信息。nextjs.org/docs/#custom-document
-
您找到解决方案了吗?可以分享一下吗?
-
嘿@Sleek,看看我的回答是否适合你
-
@Sleek 脚本正在运行,但我没有从中调用任何函数,因此它们似乎不起作用。
标签: javascript reactjs next.js