Nextjs中ts语法中使用全局变量

Nextjs中ts语法中使用全局变量

1、项目根目录增加文件cons.ts

const globalAny: any = global;

globalAny.Cons = {
    cndDomain: "https://xxxxxx"
}

export { }

增加一个声明文件,写入以下代码

declare const Cons: any;

2、_app.tsx中引入文件

import '../cons'

import type { AppProps } from 'next/app'

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />
}
export default MyApp

3、其他文件中使用

<Image src={`${Cons.cndDomain}/static/logo2.png`} alt="--" width={140} height={40} />