我正在尝试使用棱角材质创建导航栏。因此,我首先进行一些设置:
1)我在npm上安装了角形材料 2)我在style.css中配置了主题,如下所示:
/* You can add global styles to this file, and also import other style files */
@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';
@import '~https://fonts.googleapis.com/icon?family=Material+Icons';
3)我将角度材料主题链接到index.html,如下所示:
<link href="../node_modules/@angular/material/prebuilt-themes/indigo-pink.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
有关我的环境的更多信息,请查看我的angular.json文件:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"admin-portal": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"targets": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/admin-portal",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "admin-portal:build"
},
"configurations": {
"production": {
"browserTarget": "admin-portal:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "admin-portal:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"admin-portal-e2e": {
"root": "e2e/",
"projectType": "application",
"targets": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "admin-portal:serve"
},
"configurations": {
"production": {
"devServerTarget": "admin-portal:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "admin-portal"
}
但是当运行ng服务时,出现以下错误:
ERROR in ./src/styles.css (./node_modules/raw-loader!./node_modules/postcss-loader/lib??embedded!./src/styles.css) Module build failed (from ./node_modules/postcss-loader/lib/index.js): Error: Can't resolve 'https://fonts.googleapis.com/icon?family=Material+Icons' in
'C:\ Angular Projects \ admin-portal \ src' 在doResolve上(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ Resolver.js:180:19) 在hook.callAsync(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ Resolver.js:238:5) 在_fn0处(在创建时评估(C:\ Angular Projects \ admin-portal \ node_modules \ tapable \ lib \ HookCodeFactory.js:32:10), :15:1) 在resolver.doResolve(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ UnsafeCachePlugin.js:37:5) 在hook.callAsync(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ Resolver.js:238:5) 在_fn0处(在创建时评估(C:\ Angular Projects \ admin-portal \ node_modules \ tapable \ lib \ HookCodeFactory.js:32:10), :15:1) 在hook.callAsync(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ Resolver.js:238:5) 在_fn0处(在创建时评估(C:\ Angular Projects \ admin-portal \ node_modules \ tapable \ lib \ HookCodeFactory.js:32:10), :12:1) 在resolver.doResolve(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ DescriptionFilePlugin.js:42:38) 在hook.callAsync(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ Resolver.js:238:5) 在_fn5(在创建时评估(C:\ Angular Projects \ admin-portal \ node_modules \ tapable \ lib \ HookCodeFactory.js:32:10)), :57:1) 在resolver.doResolve(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ ModuleKindPlugin.js:23:37) 在hook.callAsync(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ Resolver.js:238:5) 在_fn0处(在创建时评估(C:\ Angular Projects \ admin-portal \ node_modules \ tapable \ lib \ HookCodeFactory.js:32:10), :15:1) 在hook.callAsync(C:\ Angular Projects \ admin-portal \ node_modules \ enhanced-resolve \ lib \ Resolver.js:238:5) 在_fn1处(在创建时等效(C:\ Angular Projects \ admin-portal \ node_modules \ tapable \ lib \ HookCodeFactory.js:32:10), :24:1)
答案 0 :(得分:1)
之所以不起作用,是因为当您应该使用url
函数时,您指定了该路径为本地路径。 (此外,您两次都指定了主题和字体,应该在index.html
文件中或在styles.css
文件中指定主题和字体,不要)。
您可以使用以下配置,也可以使用其他配置:
styles.css
中的所有导入:
@import '~@angular/material/prebuilt-themes/indigo-pink.css';
@import url('https://fonts.googleapis.com/icon?family=Material+Icons');
index.html
中的所有导入:
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="../node_modules/@angular/material/prebuilt-themes/indigo-pink.css">