在使用Angular 7开发Chrome扩展时,遇到mat-dialog
(Material Design对话框)在网页中可以正确呈现,但在Chrome扩展中却不能正常显示的问题。
mat-dialog
)。确保Angular Material的样式文件正确加载。可以在manifest.json
中添加样式文件的路径:
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"css": ["styles.css"]
}
],
"web_accessible_resources": [
"https://unpkg.com/@angular/material/prebuilt-themes/indigo-pink.css"
]
}
确保Angular Material的脚本在Angular应用脚本之前加载。可以在manifest.json
中调整脚本的加载顺序:
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"https://unpkg.com/@angular/material@7.0.0/bundles/material.umd.js",
"content.js"
],
"css": ["styles.css"]
}
]
}
确保Chrome扩展有足够的权限来加载所需的资源。可以在manifest.json
中添加必要的权限:
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"permissions": [
"activeTab",
"https://unpkg.com/*"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"https://unpkg.com/@angular/material@7.0.0/bundles/material.umd.js",
"content.js"
],
"css": ["styles.css"]
}
]
}
打开Chrome开发者工具的控制台,查看是否有任何错误信息。这些错误信息可以帮助定位问题。
假设你的Angular应用和Chrome扩展的基本结构如下:
manifest.json
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"permissions": [
"activeTab",
"https://unpkg.com/*"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"https://unpkg.com/@angular/material@7.0.0/bundles/material.umd.js",
"content.js"
],
"css": ["styles.css"]
}
]
}
content.js
// 初始化Angular应用
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});
styles.css
@import "https://unpkg.com/@angular/material/prebuilt-themes/indigo-pink.css";
通过以上步骤,你应该能够解决mat-dialog
在Chrome扩展中无法正常显示的问题。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云