Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 - Is it possible to use two angular element components on the same page?

I'm trying to host independent angular elements that I can drop into a non-angular webpage. It's working great when I have just one.. but when I load two or more on the same page I get this error:

Uncaught Error: Zone already loaded.

I'm using a build script to concatenate my dist files into one js file.. and then dropping that into my project

const fs = require('fs-extra') const concat = require('concat')   const files = [     './dist/elementsApp/runtime.js',     './dist/elementsApp/polyfills.js',     './dist/elementsApp/scripts.js',     './dist/elementsApp/main.js', ]   fs.ensureDir('elements') .then(() => {   console.log('success!')   concat(files, 'elements/include-me-somewhere-else.js') }) .catch(err => {   console.error(err) }) 

I can only have one of these js files on one page. I understand angular 7 will make this whole process easier but I'm wondering how I can accomplish this right now.

I've also tried wrapping my exported js file in a self-executing function to try and limit scope.. Doesn't fix anything unfortunately.

Any ideas?

People seem confused about what angular elements are. For clarification you can watch this video https://www.youtube.com/watch?v=4u9_kdkvTsc. the last few mins in particular show an angular element being exported and included on a non angular page. I'm trying to include multiple elements on the same page.

like image 579
hamobi Avatar asked May 17 '18 18:05

hamobi


People also ask

What are Webcomponents in angular?

Web components are a set of browser APIs that enable developers to create custom and reusable HTML tags that can be used in web apps just like standard HTML tags. Since Web components are built on top of standards, they can work across all modern web browsers.

Can I use Web Components in angular?

A Web Component, on the other hand, can be used anywhere. We can use it in React, Vue, Angular, or any other web application. Angular or any other frontend framework is the way to go when your components need to handle a lot of data that needs to be passed down to child components.


1 Answers

I think you should not use 2 angular on one site.

Solutions can be:

  • Build the whole site in Angular. Other parts of the code can be added to the app.
  • Use submodules and merge the two app with a parent router.
  • Use iframe for each Angular component. Then ngzone will not collide.
like image 181
androbin Avatar answered Sep 22 '22 13:09

androbin