简体   繁体   English

Aurelia:如何使用 npm 包中的视图/视图模型?

[英]Aurelia: How to use a View/Viewmodel from an npm package?

We are using Aurelia for our application's frontend.我们将 Aurelia 用于我们应用程序的前端。 As we will have several different projects based on it, I would like to be able to add all of our custom code to some npm packages that could be added by the developers.由于我们将有几个基于它的不同项目,我希望能够将我们所有的自定义代码添加到开发人员可以添加的一些 npm 包中。 This would allow us to create a new empty project, add the dependencies to our reusable code without including it in the project's code base (so it can be updated separately if needed).这将允许我们创建一个新的空项目,将依赖项添加到我们的可重用代码中,而不将其包含在项目的代码库中(因此可以在需要时单独更新)。

For instance, I would like a tools package and a service package.例如,我想要一个工具包和一个服务包。 This is of course quite easy.这当然很容易。

But I can't figure out how to use a 'ui' package that would contain all our custom reusable components.但我不知道如何使用包含我们所有自定义可重用组件的“ui”包。 Is that even possible?这甚至可能吗? How would I require a component in an html template?我如何在 html 模板中需要一个组件?

If this can't be done, does anyone have an idea of how to cleanly separate the reusable code from the application specific code?如果无法做到这一点,有没有人知道如何将可重用代码与应用程序特定代码彻底分开?

thanks a lot!多谢!

of course you can, that's what a lot of the plugins available for aurelia doing.当然可以,这就是许多可用于 aurelia 的插件所做的。 One way is to register your components as global resources (in your package or plugin) and import them in your client app, CLI example:一种方法是将您的组件注册为全局资源(在您的包或插件中)并将它们导入您的客户端应用程序,CLI 示例:

// from your plugin
aureliaConfig.globalResources([
    './jqm-loader',
    './jqm-page',
    './jqm-footer',
    './jqm-header'
]);

then import them in your app:然后将它们导入您的应用程序:

// aurelia.json
{
    "name": "my-reusable-widgets",
    "path": "../node_modules/my-reusable-widgets",
    "main": "index",
    "resources": [
          "**/*.{css,html}" //to load them all or you can add individual files
     ]
}

then use your widgets:然后使用您的小部件:

<jqm-loader></jqm-loader>
...

if you don't want to use globalResources you can also use require:如果你不想使用 globalResources 你也可以使用 require:

<require from="my-reusable-widgets/jqm-header"></require>
<jqm-header></jqm-header>

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

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