我已从 Cordova 升级到 Capacitor 并使用 Angular。 我的 Variables.scss 文件中的主题没有被使用。 它使用的是 node_modules/@ionic/angular/css/core.css 中的主题。
因此,当对下面的颜色应用“primary”时,它不会使用variables.scss文件中设置的“ion-color-primary”。 它使用 core.css 文件中设置的颜色。
<ion-header mode="md"
class="ion-no-border">
<ion-toolbar color="primary">
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
我错过了什么?
文件夹结构在src/theme中有variables.scss
我有一个变量.scss:
:root {
--ion-color-primary: #007eae;
--ion-color-primary-rgb: 0,126,174;
--ion-color-primary-contrast: #ffffff;
--ion-color-primary-contrast-rgb: 255,255,255;
--ion-color-primary-shade: #006f99;
--ion-color-primary-tint: #1a8bb6;
--ion-color-secondary: #0cd1e8;
--ion-color-secondary-rgb: 12,209,232;
--ion-color-secondary-contrast: #ffffff;
--ion-color-secondary-contrast-rgb: 255,255,255;
--ion-color-secondary-shade: #0bb8cc;
--ion-color-secondary-tint: #24d6ea;
电容.config.ts:
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
appId: 'myappid',
appName: 'myapp',
webDir: 'www',
server: {
androidScheme: 'https',
allowNavigation: ['*']
},
plugins: {
PushNotifications: {
presentationOptions: ["badge", "sound", "alert"]
},
},
bundledWebRuntime: true,
cordova: {
preferences: {
WKWebViewOnly: 'true',
'android-minSdkVersion': '29',
'android-targetSdkVersion': '34',
'deployment-target': '15.5',
ScrollEnabled: 'false',
BackupWebStorage: 'none',
SplashMaintainAspectRatio: 'true',
FadeSplashScreenDuration: '300',
SplashShowOnlyFirstTime: 'false',
SplashScreen: 'screen',
SplashScreenDelay: '3000',
loadUrlTimeoutValue: '120000',
ShowSplashScreenSpinner: 'false',
AndroidLaunchMode: 'singleTask',
AndroidXEnabled: 'true',
AndroidPersistentFileLocation: 'Compatibility',
StatusBarOverlaysWebView: 'false',
StatusBarBackgroundColor: '#00527e',
AndroidInsecureFileModeEnabled: 'true'
}
}
};
export default config;
角度.json:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "webpack.config.js"
},
"outputPath": "www",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"assets": [
{
"glob": "**/*",
"input": "src/assets",
"output": "assets"
},
{
"glob": "**/*.svg",
"input": "node_modules/ionicons/dist/ionicons/svg",
"output": "./svg"
}
],
"styles": [
"src/theme/variables.scss",
"src/global.scss",
"node_modules/@ionic/angular/css/core.css",
"node_modules/@ionic/angular/css/normalize.css",
"node_modules/@ionic/angular/css/structure.css",
"node_modules/@ionic/angular/css/typography.css",
"node_modules/@ionic/angular/css/display.css",
"node_modules/@ionic/angular/css/padding.css",
"node_modules/@ionic/angular/css/float-elements.css",
"node_modules/@ionic/angular/css/text-alignment.css",
"node_modules/@ionic/angular/css/text-transformation.css",
"node_modules/@ionic/angular/css/flex-utils.css"
],
"scripts": [
"src/assets/sql.js",
"src/assets/base_64.js"
],
"aot": true,
"extractLicenses": false,
"buildOptimizer": false,
"sourceMap": true,
"optimization": false,
"namedChunks": true,
"allowedCommonJsDependencies": [
"qrcode",
"moment",
"date-fns-tz"
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "8mb",
"maximumError": "8mb"
}
]
},
"ci": {
"progress": false
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"buildTarget": "app:build"
},
"configurations": {
"production": {
"buildTarget": "app:build:production"
},
"ci": {
"progress": false
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"buildTarget": "app:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"styles": [],
"scripts": [],
"assets": [
{
"glob": "favicon.ico",
"input": "src/",
"output": "/"
},
{
"glob": "**/*",
"input": "src/assets",
"output": "/assets"
}
]
},
"configurations": {
"ci": {
"progress": false,
"watch": false
}
}
},
"lint": {
"builder": "@angular-eslint/builder:lint",
"options": {
"lintFilePatterns": [
"src/**/*.ts",
"src/**/*.html"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "app:serve"
},
"configurations": {
"production": {
"devServerTarget": "app:serve:production"
},
"ci": {
"devServerTarget": "app:serve:ci"
}
}
}
}
}
},
"cli": {
"analytics": false,
"schematicCollections": [
"@angular-eslint/schematics"
]
},
"schematics": {
"@ionic/angular-toolkit:component": {
"styleext": "scss"
},
"@ionic/angular-toolkit:page": {
"styleext": "scss"
}
}
}
看起来您的variables.scss已正确设置,但您的样式加载或覆盖方式可能存在问题。您可以检查以下几项来解决问题:
1。确保variables.scss正确加载:
您的 angular.json 文件包含变量.scss,这很好,但请确保它列在其他 Ionic 样式表之前。在您当前的配置中,它已经是列表中的第一个样式表,因此这应该可以工作,但请仔细检查 src/theme/variables.scss 是否正在正确处理。
2。使用ionic.config.json进行主题设置:
如果您要覆盖 Ionic 的默认主题变量,请确保使用正确的配置并确保其他导入的文件不会产生冲突。
在 ionic.config.json 中,如有必要,您可以明确提及变量.scss 的路径:
{
"name": "my-app",
"type": "angular",
"integrations": {
"capacitor": {}
},
"theme": {
"variables": "src/theme/variables.scss"
}
}
3.检查 src/theme/variables.scss 中是否缺少变量:
确保您的变量定义正确并且没有拼写错误或问题。格式应该是这样的:
:root {
--ion-color-primary: #007eae;
--ion-color-primary-rgb: 0,126,174;
--ion-color-primary-contrast: #ffffff;
--ion-color-primary-contrast-rgb: 255,255,255;
--ion-color-primary-shade: #006f99;
--ion-color-primary-tint: #1a8bb6;
}
此外,检查其他 CSS 文件中可能发生的任何 ion-color-primary 覆盖。
4。检查浏览器的开发者工具:
使用浏览器的开发人员工具(例如 Chrome DevTools)检查应用于离子工具栏的样式并检查原色的来源。如果应用了 core.css,则可能会在那里设置颜色,从而覆盖您的自定义主题。查找 --ion-color-primary 变量并确保正确应用它。
5。清除缓存并重建应用程序:
有时,由于缓存的原因,样式表中的更改可能不会立即生效。尝试清除浏览器缓存或重建应用程序:
npm run build
npx cap sync
6。自定义 Webpack 配置:
由于您使用的是自定义 Webpack 配置,请确保 Variables.scss 正确包含在您的 Webpack 设置中。您可能需要修改 webpack.config.js 以将其包含为所有组件的全局样式表。
SCSS 的 Webpack 配置示例:
module.exports = {
// Other configurations...
module: {
rules: [
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
additionalData: `@import "src/theme/variables.scss";`
}
}
]
}
]
}
};
7。检查范围问题:
如果您仍然遇到问题,请确保样式的范围不正确。例如,如果您使用 Angular 的 ViewEncapsulation,它可能会阻止应用全局样式。您可以通过将以下内容添加到组件来禁用它:
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-my-component',
encapsulation: ViewEncapsulation.None,
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.scss']
})
8。调试技巧 - 覆盖组件中的样式:
作为临时调试步骤,尝试使用组件样式表中的 CSS 自定义属性直接应用样式:
ion-toolbar {
--background: var(--ion-color-primary);
}
这些步骤应有助于确保您的自定义主题在您的应用程序中正确应用。如果您需要进一步帮助,请告诉我!