简体   繁体   English

如何向现有的Angular 5项目中添加更少内容

[英]How To Add Less To Existing Angular 5 Project

I have this JS Fiddle for a CSS chart. 我有这个JS Fiddle用于CSS图表。 How do I add LESS to an existing Angular 5 project so that I can utilize this chart, and where do I place the JavaScript and references to the LESS file from this example in my Angular component? 如何将LESS添加到现有的Angular 5项目中,以便可以利用此图表,以及如何将本示例中的JavaScript和对LESS文件的引用放在Angular组件中? JSFiddle included below. JSFiddle包含在下面。

JavaScript: JavaScript的:

$('head style[type="text/css"]').attr('type', 'text/less');
less.refreshStyles();

http://jsfiddle.net/andydesrosiers/fwaLt99a/ http://jsfiddle.net/andydesrosiers/fwaLt99a/

A typical Angular component has a html, a typescript and a style file. 典型的Angular组件具有html,打字稿和样式文件。 The later is usually css: 后者通常是CSS:

my-comp.component.html
my-comp.component.ts
my-comp.component.less     <-- by default, is css

You want to tell angular to look for a .less file instead of .css and compile it during the build process. 您想要告诉angular寻找一个.less文件而不是.css文件,并在构建过程中对其进行编译。 To do this: 去做这个:

  • Edit the file .angular-cli.json in your project. 在您的项目中编辑文件.angular-cli.json Set the property defaults.styleExt to "less" . 将属性defaults.styleExt"less"

  • For every existing component that uses css and you want to change to less, rename the style file and make sure that the typescript file points to the correct stylesheet: 对于每个使用css的现有组件,并且您希望更改为更少,请重命名样式文件,并确保打字稿文件指向正确的样式表:

     @Component({ selector: 'app-my-comp', templateUrl: './my-comp.component.html', styleUrls: ['./my-comp.component.less'] }) 

I have not the best solution, but it works for me. 我没有最好的解决方案,但是对我有用。 As I don't have a node server, what I do is have de scss file in project. 由于我没有节点服务器,所以我要做的是在项目中有dessss文件。 I compile it online, and it gives me the css and I put it on the project again. 我在线编译它,它给了我CSS,然后再次将其放在项目中。 It still with both files but the scss (less in your case) stays there and when I need more css I code it normaly but always add on scss too. 它仍然同时包含两个文件,但scss(在您的情况下较少)停留在那里,当我需要更多css时,我通常对其进行编码,但也总是添加scss。 So I compile it e overwrite the older css again. 所以我编译它再次覆盖了旧的CSS。 It's not so safe but works very well when you can't add a new server to solve your dynamic styling problems. 它不是很安全,但是当您无法添加新服务器来解决动态样式问题时,效果很好。 But if you are on a big project maybe it's better to add a node server or something like to have your less working safely. 但是,如果您正在从事大型项目,则最好添加节点服务器或类似的方法,以减少安全的工作量。

Andy, one of my previous statements was incorrect Angular supports many css preprocessors including less. Andy,我先前的陈述之一是错误的Angular支持许多CSS预处理器,包括更少的。 Now that I know this here is one possible solution to your problem. 现在,我知道这是解决您的问题的一种可能的方法。 You can generate a component like below. 您可以生成如下所示的组件。

-voltage-meter.component.ts - 电压,meter.component.ts

import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';

@Component({
  selector: 'app-voltage-meter',
  templateUrl: './voltage-meter.component.html',
  styleUrls: ['./voltage-meter.component.less'],
  encapsulation: ViewEncapsulation.None
})
export class VoltageMeterComponent implements OnInit {
  @Input() voltage: number;
  constructor() { }

  ngOnInit() {
    console.log(this.voltage);
  }

}

-voltage-meter.component.less (just paste your less code in this file) -voltage-meter.component.less(只需将较少的代码粘贴到此文件中)

-voltage-meter.component.html - 电压,meter.component.html

<div class="radial-progress" attr.data-progress='{{voltage}}'>
    <div class="circle">
        <div class="mask full">
            <div class="fill"></div>
        </div>
        <div class="mask half">
            <div class="fill"></div>
            <div class="fill fix"></div>
        </div>
        <div class="shadow"></div>
    </div>
    <div class="inset">
        <div class="voltage"><div class="voltage-in-use">Voltage in use</div></div>
    </div>
</div>

Then you can use the component using 然后您可以使用

<app-voltage-meter [voltage]='12'></app-voltage-meter>

I got this working using the Angular cli to generate the project assigning it the less preprocessor 我使用Angular cli进行了这项工作,以生成为其分配较少预处理器的项目

ng new less-test --style=less

That would allow you to use the less code you have in your jsfiddle example. 那将允许您使用jsfiddle示例中较少的代码。

Apologize again for my erroneous statement earlier today and thanks to seven-phases-max for pointing out my error. 对于今天早些时候的错误陈述,我们再次道歉,并感谢7个最大阶段指出了我的错误。

Edit: For an existing project follow this article just replace scss with the preprocessor you are using. 编辑:对于现有项目,请遵循本文,只需将scss替换为您使用的预处理器即可。

Angular-cli from css to scss 从CSS到CSS的Angular CLI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM