如何解决如何在 shadow dom 内运行 angular?
如何在 shdow dom 中运行 angular 应用程序?
代码:
<script type="text/javascript">
customElements.define('show-hello',class extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({mode: 'closed'});
shadow.innerHTML = `
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="http://domain/styles.5b06983da863c73ff6ae.css">
<app-root></app-root>
`;
var tag = document.createElement('script');
tag.src = "http://domain/runtime.c5b03efbbe032268e2db.js";
tag.defer = true;
shadow.append(tag);
tag = document.createElement('script');
tag.src = "http://domain/polyfills-es5.1a4928232678b73b212e.js";
tag.nomodule = true;
tag.defer = true;
shadow.append(tag);
tag = document.createElement('script');
tag.src = "http://domain/polyfills.5d56ab2a8a4492384195.js";
tag.defer = true;
shadow.append(tag)
tag = document.createElement('script');
tag.src = "http://domain/scripts.cb9c1c75fdd88cce0ad3.js";
tag.defer = true;
shadow.append(tag)
tag = document.createElement('script');
tag.src = "http://domain/main.8120316e488535e5c9fe.js";
tag.defer = true;
shadow.append(tag)
console.log(shadow.innerHTML);
}
});
</script>
<show-hello></show-hello>
错误:
ERROR Error: The selector "app-root" did not match any elements
at t.selectRootElement (main.8120316e488535e5c9fe.js:1)
at t.selectRootElement (main.8120316e488535e5c9fe.js:1)
at main.8120316e488535e5c9fe.js:1
at e.create (main.8120316e488535e5c9fe.js:1)
at t.bootstrap (main.8120316e488535e5c9fe.js:1)
at main.8120316e488535e5c9fe.js:1
at Array.forEach (<anonymous>)
at t._moduleDoBootstrap (main.8120316e488535e5c9fe.js:1)
at main.8120316e488535e5c9fe.js:1
at e.invoke (polyfills.5d56ab2a8a4492384195.js:1)
如果这是不可能的,也许有其他方法可以将应用程序嵌入到站点中,使应用程序样式不与站点样式混合?
解决方法
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<base href="/"> <!-- Important for Angular App -->
<!-- There are react app links etc. -->
</head>
<body>
<!-- I just split view for two Apps -->
<div style="display: flex; flex-direction: row;">
<div style="width: 50vw;" id="root"></div> <!-- It's for React App -->
<app-root style="width: 50vw;"> <!-- This should be plased in real DOM -->
<first-app></first-app> <!-- We also need put a shadow component here -->
</app-root>
</div>
</div><script>
// There is react app code form index.html
</script>
<script type="text/javascript">
// This is our script for Angular app
// Notice that the distribution is in the folder: dist/my-app/
customElements.define('first-app',class extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({mode: 'open'});
const scripts = ['runtime.js','polyfills.js','vendor.js','main.js'];
scripts.forEach(name => {
const tag = document.createElement('script');
tag.src = 'dist/my-app/' + name;
tag.nomodule = true;
tag.defer = true;
shadow.append(tag);
})
}});
</script>
</body>
</html>
这是一个关于在 shadow 组件中保留自己的样式的示例。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
customElements.define('show-hello',class extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({mode: 'open'});
const styles = `
<style>
p{color: blue;}
</style>
`
shadow.innerHTML = `
${styles}
<p>
Hello,${this.getAttribute('name')}
</p>`;
}
});
</script>
<p>It is not in shadow</p>
<show-hello name="from shadow"></show-hello>
</body>
</html>
这个link也可能有帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。