I have built an aurelia v2 app and would like to add it to an existing website.
I have gotten this to work by copying the created dist folder into the root of the website so it would look something like this:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script defer="defer" src="dist/entry.6d5787857fb220b5df4b.bundle.js"></script>
</head>
<body>
<div style="background: lightblue; height:300px; display: flex; align-items:center;">
THIS IS THE HEADER
</div>
<my-app></my-app>
<div style="background: lightblue; height:300px; display: flex; align-items:center;">
THIS IS THE FOOTER
</div>
</body>
</html>
Everything seems to be working and the app is displaying and working correctly. When this script entry.6d5787857fb220b5df4b.bundle.js is loaded, then it looks for a my-app tag and bootstraps the aurelia app into that.
Now, I assuming that this is not the best way to do this. Has anyone tried something like this?
Maybe the aurelia app can be rendered inside a web component, which can the be added to an external webpage or every nested within another application that was built using react or angular.
render an Aurelia app. All it requires is a host element and a root component. This means in the Angular app, you will get some element reference, and instantiate an Aurelia app with this element as host.
As you notice, if you want to simplify the process of creating an element, any do not want to instantiate Aurelia apps in many places, you can use native web component to do this.
Hi @ivan! I think what you are doing is also perfectly alright. If you want to render from a WebComponent, then I think it might not be that different. IMO the process is roughly as follows:
Create a “root” element to host the aurelia app.
Attach that root to the DOM (or for WC might ShadowDOM).
Use this element to host the aurelia app as usual.
So I got a custom web component to work by adding this to the main.js file of my project.
import { Aurelia, DialogDefaultConfiguration } from 'aurelia'
import { MyApp } from './my-app'
class MyComponent extends HTMLElement {
constructor() {
super()
}
connectedCallback() {
Aurelia
.register(DialogDefaultConfiguration)
.app({
component: MyApp,
host: this
})
.start()
}
}
customElements.define('my-component', MyComponent)
With a couple of small changes to the webpack config, this now allows me to publish the build to npm, import it into another project and use the custom element.
In the code example above, I am not using a shadow dom. I did some initial experiments, but could not get the styles to load correctly. Seems that Aurelia always adds the styles to the <head> tag of the document body.
Is there a way to add the css directly to the component element?
Is there a way to add the css directly to the component element?
I think the convention is to add the styles in head. However, this is more or less a bundling aspect as far as I understand. @huochunpeng is there a way to customize it?
Although, if you are trying to restrict the styles to your individual custom element, you may want to use CSS modules that renames your CSS selectors uniquely. With that even if the styles are added globally, it should not pose any conflicts.