首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在无CLI的角度分量中使用更少的

问如何在无CLI的角度分量中使用更少的
EN

Stack Overflow用户
提问于 2019-10-12 05:25:40
回答 1查看 1.5K关注 0票数 3

我有一个应用程序,在这个应用程序中,我必须为动态主题实现配置和使用更少的内容。问题是,我们没有使用角-cli,配置在这里有点奇怪,所以我们手动引导角模块。

以下是app的配置:

package.json

代码语言:javascript
复制
      "dependencies": {
        "@angular/common": "^4.0.0",
        "@angular/compiler": "^4.0.0",
        "@angular/core": "^4.0.0",
        "@angular/forms": "^4.0.0",
        "@angular/http": "^4.0.0",
        "@angular/platform-browser": "^4.0.0",
        "@angular/platform-browser-dynamic": "^4.0.0",
        "@angular/router": "^4.0.0",
        "@angular/upgrade": "^4.0.0",
        "@angular/animations": "^4.0.1",
        "bootstrap": "^3.3.7",
        "core-js": "^2.4.1",
        "reflect-metadata": "^0.1.8",
        "rxjs": "^5.0.1",
        "systemjs": "0.19.39",
        "zone.js": "^0.8.4"
      },
and so on..

main.ts

代码语言:javascript
复制
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
import { ThemeModule } from "./theme.module";

window.appnamespace.platform = platformBrowserDynamic();
window.appnamespace.AppModule = AppModule
window.appnamespace.ThemeModule = ThemeModule;

theme.module.ts

代码语言:javascript
复制
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";

import { ThemeComponent } from "./theme.component";
import { ThemeToolbar } from "./Themes/theme-toolbar/theme-toolbar.component";
import { ThemePreview } from "./Themes/theme-preview/theme-preview.component";
import { ThemeService } from "./services/themeManagement.service";

const PERFECT_SCROLLBAR_CONFIG: PerfectScrollbarConfigInterface = {
  suppressScrollX: true
};

@NgModule({
  imports: [
    BrowserModule,
  ],
  declarations: [
    ThemeComponent,
    ThemeToolbar,
    ThemePreview,
    SetFocusDirective
  ],
  providers: [ThemeService, LocalizeThemeService, CommonService],
  bootstrap: [ThemeComponent]
})

export class ThemeModule { }

its组件:

代码语言:javascript
复制
@Component({
  selector: "my-theme",
  templateUrl: "../js/divdrawer/Themes/theme.template.html"
})
export class ThemeComponent implements OnInit {
  //
}

并通过javascript引导它,如: window.appnamespace.platform.bootstrapModule(window.appnamespace.ThemeModule);:

theme.preview组件

代码语言:javascript
复制
@Component({
  selector: "theme-preview",
  templateUrl: "../theme-preview/theme-preview.component.template.html",
  styleUrls: ['../theme-preview/theme-style.less']
})

export class ThemePreview implements OnInit {
  // some code
}

theme-style.less:包含css

代码语言:javascript
复制
@import "./theme-variable.less";

// some css

theme-variable.less:包含较少的变量

代码语言:javascript
复制
@header-bg        :red;
@badge-title-bg   : #ddd;

我希望在主题预览组件中使用较少的变量和样式来动态更改主题。

如何仅在主题组件中配置更少的内容。

EN

回答 1

Stack Overflow用户

发布于 2019-10-25 11:08:17

在您的@Component中,属性styleUrl拥有所有需要合并到一个CSS文件中的样式文件。然而,这是由角CLI完成的。

如果您打算使用CLI从less生成CSS,请转到名为angular.json的文件,并将schematics.@schematics/angular:component.style的值更改为'less'。

然而,这可能无助于动态(运行时)主题化。动态主题化,

  1. 使用@import语句在任何一个主文件
  2. 中导入所有较少的文件,用于服务器端编译,在请求CSS时使用node.js server和less.js编译它。因此,less.js将是您的node.js项目中的生产依赖项。
  3. 用于客户端编译,将步骤1中创建的主减文件放入html的head部分如下。<link href="main.less" rel="stylesheet" type="text/less"/>下一步,将less.js导入到角应用程序的main.ts中,或者在HTML中为less.js使用脚本标记。

在@Component中,可以删除styleUrls,因为它没有使用。

此外,所有与组件相关的样式都需要包装在一个特定于组件的类中(包括HTML和更少)。这将确保您实现与角CLI相同的行为。

希望这能有所帮助。如有任何更具体的问题,请评论。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58351135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档