简体   繁体   English

Stencil JS - 如何分发一组共享方法

[英]Stencil JS - How to also distribute a set of shared methods

I have this simple component that is compiled to a web-component by Stencil:我有一个由 Stencil 编译为 Web 组件的简单组件:

import { Component, h, Prop } from "@stencil/core";
import { IAuthLoginConfig } from "../../interfaces";
import { initAuth } from "../../services";

@Component({
  tag: "login-button",
  styleUrl: "login-button.css",
})
export class LoginButton {
  @Prop() public baseUrl: string = "/oidc/v1/authorize";

  @Prop() public config: IAuthLoginConfig;

  render() {
    return (
      <button onClick={() => initAuth(this.config, this.baseUrl)}>
        Sign in
      </button>
    );
  }
}

On the button click a shared function initAuth(...) is called that this is imported from the services-directory:在按钮上单击共享的 function initAuth(...)被称为这是从服务目录导入的:

import { IAuthLoginConfig } from "../interfaces";

export const initAuth = (authConfig: IAuthLoginConfig, baseUrl: string): void => {
  const url = `${baseUrl}${buildParameters(authConfig)}`;

  window.location.href = url

};

const buildParameters = ({ oauth2, oidc }: IAuthLoginConfig) => {
  return 'some parameters';
};

Is there any (standard Stencil) way to also build and publish this file so that a user of our web-component library can import the exported functions and use/call them?是否有任何(标准 Stencil)方法来构建和发布此文件,以便我们的 Web 组件库的用户可以导入导出的函数并使用/调用它们? In our use-case an end-user should be able to use methods in his/her own application directly that are also used in our web-components.在我们的用例中,最终用户应该能够直接在他/她自己的应用程序中使用方法,这些方法也用于我们的 Web 组件。

Other use-cases: shared variables, classes...其他用例:共享变量、类...

Thanks in advance!提前致谢!

You will have to manually export each object you want to be accessible in ./src/index.ts , eg:您必须手动导出您希望在 ./src/index.ts 中访问的每个./src/index.ts ,例如:

export { initAuth } from './services';
// or
export * from './services';

This allows you to import it in the consuming project:这允许您将其导入到使用项目中:

import { initAuth } from 'your-installed-package';
// or (depending on how you published)
import { initAuth } from 'your-installed-package/dist';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM