返回

在 Nuxt 3 中集成 preline 库

开发配置

  1. 安装 preline 库:
npm install preline --save
  1. 在你的项目根目录创建一个名为 preline.client.js 的文件,并添加以下代码:
import "preline/preline";
import { type IStaticMethods } from "preline/preline";

declare global {
  interface Window {
    HSStaticMethods: IStaticMethods;
  }
}

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.hook("page:finish", () => {
    window.HSStaticMethods.autoInit();
  });
});
  1. 在你的 nuxt.config.js 文件中,在 buildModules 数组中添加 preline
export default {
  // ...
  buildModules: [
    // ...
    'preline'
  ],
  // ...
}
  1. 在你的项目中,你可以通过 window.HSStaticMethods 访问 preline 库的静态方法。例如:
window.HSStaticMethods.identify('user-id', {
  email: '[email protected]',
  name: 'John Doe'
});
  1. 当你的页面加载完成后,preline 库将自动初始化。