gpt4 book ai didi

javascript - 当我更新 "@angular-devkit/build-angular"时样式加载器停止工作

转载 作者:行者123 更新时间:2023-12-03 06:59:44 30 4
gpt4 key购买 nike

我在angular8工作并面临一个问题,

为了动态加载我的样式,我使用了下面的代码,它工作正常,但是当我更新包时 "@angular-deficit/build-angular": "^0. 800.0""@angular-deficit/build-angular": "^0. 803.21" ,然后它停止工作。该应用程序运行良好,终端和浏览器控制台中没有错误,但应用程序未实现样式。

declare function require(name: string): any;
ngOnInit() {
require('style-loader!./file-path');
}

包.json
{
"name": "latest-frontend-theme",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"bundle-report": "webpack-bundle-analyzer dist/stats.json"
},
"private": true,
"dependencies": {
"@agm/core": "^1.1.0",
"@angular/animations": "^8.2.14",
"@angular/cdk": "^8.2.3",
"@angular/common": "~8.2.14",
"@angular/compiler": "~8.2.14",
"@angular/core": "~8.2.14",
"@angular/forms": "~8.2.14",
"@angular/material": "^8.2.3",
"@angular/platform-browser": "~8.2.14",
"@angular/platform-browser-dynamic": "~8.2.14",
"@angular/router": "~8.2.14",
"@ngx-loading-bar/core": "^4.2.0",
"@ngx-loading-bar/router": "^4.2.0",
"angular2-text-mask": "^9.0.0",
"crypto-js": "^3.1.9-1",
"file-saver": "^2.0.2",
"moment": "^2.24.0",
"mydatepicker": "^2.6.8",
"ng2-ckeditor": "^1.2.6",
"ng2-file-upload": "^1.3.0",
"ng6-toastr": "^6.0.0",
"ngx-google-places-autocomplete": "^2.0.4",
"ngx-slick": "^0.2.1",
"rxjs": "~6.5.3",
"rxjs-compat": "^6.5.3",
"tslib": "^1.9.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.803.21",
"@angular/cli": "~8.3.21",
"@angular/compiler-cli": "~8.2.14",
"@angular/language-service": "~8.2.14",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.8",
"@types/node": "~12.12.21",
"codelyzer": "^5.2.1",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.4.1",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~2.1.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~8.5.4",
"tslint": "~5.20.1",
"typescript": "~3.4.3",
"webpack-bundle-analyzer": "^3.6.0"
}

}

任何形式的帮助都是可观的。

最佳答案

我今天遇到了同样的问题,但仍然无法使用 style-loader 找到解决方案所以我想出了一个替代解决方案,我将分享,因为这仍然没有答案。
我以前使用 style-loader像这样:

// in the AppComponent
private initTheme(settings: Settings) {

if (settings.style) {
// set specific theme
require(`style-loader!./../../src/${settings.style.id}-theme.scss`);
}
else {
require(`style-loader!./../../src/default-theme.scss`);
}
}
我的解决方案是更新此函数以手动应用样式表(作为已处理的 CSS 文件),如下所示:
// in the AppComponent
private initTheme(settings: Settings) {

const applyStyleSheet = (styleName: string) => {
const head = document.getElementsByTagName('head')[0];
const style = document.createElement('link');
style.rel = 'stylesheet';
style.href = `${styleName}.css`;
head.appendChild(style);
};

if (settings.style) {
// set specific theme
applyStyleSheet(`${settings.style.id}-theme`);
}
else {
applyStyleSheet(`default-theme`);
}
}
为了处理 SCSS 文件,我更改了我的 angular.json文件,添加 extractCss到选项:
"options": {
"extractCss": true
...
}
... 并在 styles 中指定每个 SCSS 文件要处理但未注入(inject)的数组:
"styles": [
{
"input": "src/default-theme.scss",
"bundleName": "default-theme",
"inject": false
},
{
"input": "src/basic-theme.scss",
"bundleName": "basic-theme",
"inject": false
}
...
]

关于javascript - 当我更新 "@angular-devkit/build-angular"时样式加载器停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59478096/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com