GVKun编程网logo

Angular 2,TypeScript中的sprintf(angularjs typescript)

15

在本文中,您将会了解到关于Angular2,TypeScript中的sprintf的新资讯,同时我们还将为您解释angularjstypescript的相关在本文中,我们将带你探索Angular2,T

在本文中,您将会了解到关于Angular 2,TypeScript中的sprintf的新资讯,同时我们还将为您解释angularjs typescript的相关在本文中,我们将带你探索Angular 2,TypeScript中的sprintf的奥秘,分析angularjs typescript的特点,并给出一些关于Angular 12 更新 - Angular 编译器需要 TypeScript、Angular 2 typescript调用javascript函数、Angular 2 – typescript中的window.print是什么、Angular 5.将类导入到彼此时,在typescript中的循环依赖的实用技巧。

本文目录一览:

Angular 2,TypeScript中的sprintf(angularjs typescript)

Angular 2,TypeScript中的sprintf(angularjs typescript)

有人能告诉我在Angular 2 Typescript组件中使用完整的sprintf库(零填充和标准sprintf中通常存在的所有其他功能)所需的所有步骤吗?谢谢.

解决方法

安装sprintf-js

npm install sprintf-js --save

安装TypeScript定义

typings install dt~sprintf-js --global --save

导入并使用代码

import {sprintf} from "sprintf-js";

console.log(sprintf("%s %s!","Hello","Massimo"));

Angular 12 更新 - Angular 编译器需要 TypeScript

Angular 12 更新 - Angular 编译器需要 TypeScript

如何解决Angular 12 更新 - Angular 编译器需要 TypeScript?

我已将我的项目更新到最新的 angular 版本。 使用“ng update”更新后,我收到消息,一切都是最新的。 (我流程的最后一步,需要的包已经更新了)

很好,但是当我为项目提供服务时,出现以下错误:

Error: The Angular Compiler requires TypeScript >=4.2.3 and <4.3.0 but 4.3.2 was found instead. 

我已经尝试过:npm install typescript@4.3.0 但那个版本不存在。我宁愿不降级。我努力让我的项目保持最新状态。

我该如何解决这个问题。

解决方法

正如评论已经指出的那样,Angular 目前仅支持 typescript 4.2。最新的支持版本可以安装

npm i typescript@4.2.*

如果您确实想要使用最新的打字稿,可以在您的 tsconfig 中设置标志 disableTypeScriptVersionCheck(虽然不推荐): https://angular.io/guide/angular-compiler-options#disabletypescriptversioncheck

,

Angular 12 仅支持 Typescript 4.2。 您必须像这样修改您的 package.json

    "typescript": "~4.2.3"

并重新运行npm install

Angular 2 typescript调用javascript函数

Angular 2 typescript调用javascript函数

是否有正确的方法从Angular 2(TypeScript)中的组件调用 JavaScript函数?

这是我的组件:

import { ElementRef,AfterViewInit }       from '@angular/core';

export class AppComponent implements AfterViewInit {

    constructor(private _elementRef: ElementRef) {
    }

    ngAfterViewInit() {
        /**
         * Works but i have this error :
         * src/app.component.ts(68,9): error TS2304: Cannot find name 'MYTHEME'.
         * src/app.component.ts(69,9): error TS2304: Cannot find name 'MYTHEME'.
         */
        MYTHEME.documentOnLoad.init(); 
        MYTHEME.documentOnReady.init();

        /**
         * Works without error,but doesn't seem like a right way to do it
         */
        var s = document.createElement("script");
        s.text = "MYTHEME.documentOnLoad.init(); MYTHEME.documentOnReady.init();";
        this._elementRef.nativeElement.appendChild(s);
    }
}

直接调用JavaScript函数会导致编译错误,但“已编译”的JavaScript文件(app.component.js)中的语法是正确的:

AppComponent.prototype.ngAfterViewInit = function () {
    MYTHEME.documentOnLoad.init();
    MYTHEME.documentOnReady.init();
};

第二种方式(appendChild)工作没有错误,但我不认为(从typescript / angular更改DOM)是正确的方法.

我发现了这个:Using a Javascript Function from Typescript我尝试声明界面:

interface MYTHEME {
    documentOnLoad: Function;
    documentOnReady: Function;
}

但是TypeScript似乎没有识别它(接口声明中没有错误).

谢谢

编辑:

在Juan Mendes的回答之后,这就是我的结局:

import { AfterViewInit }       from '@angular/core';

interface MYTHEME {
    documentOnLoad: INIT;
    documentOnReady: INIT;
}
interface INIT {
    init: Function;
}
declare var MYTHEME: MYTHEME;

export class AppComponent implements AfterViewInit {

    constructor() {
    }

    ngAfterViewInit() {
        MYTHEME.documentOnLoad.init(); 
        MYTHEME.documentOnReady.init();
    }
}
您必须使用declare告诉TypeScript有关外部(JavaScript)声明的信息.见 https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html
interface MyTheme {
    documentOnLoad: Function;
    documentOnReady: Function;
}
declare var MYTHEME: MyTheme;

或者匿名

declare var MYTHEME: {documentOnLoad: Function,documentOnReady: Function};

Angular 2 – typescript中的window.print是什么

Angular 2 – typescript中的window.print是什么

在 javascript我可以做:

window.print()

打开打印机对话框,但是如何在打字稿中执行此操作?

解决方法

(window as any).print();

如果不使用typescript 2.8.2,请使用以上行.

Angular 5.将类导入到彼此时,在typescript中的循环依赖

Angular 5.将类导入到彼此时,在typescript中的循环依赖

在使用TypeScript的Angular 5应用程序中.当尝试实现组件之间的通信时,我遇到了称为循环依赖的问题.
有两个分量无线电和无线电组:

<radio-group [(value)]='selected'>
  <radio value='1'></radio>
  <radio value='2'></radio>
  <radio value='3'></radio>
</radio-group>

当用户选择和取消选择项目时,它们彼此通信.

组件实现示例
RadioGroupComponent:

import { Component,forwardRef,Input,Optional,ContentChildren,QueryList,EventEmitter,Output,ChangeDetectorRef,ChangeDetectionStrategy,AfterContentinit,OnChanges } from '@angular/core';
import { RadioGroup } from './radio-group.component';

@Component({
  selector: 'radio',styles: [`:host{cursor:pointer;}`],template: `<div (click)='check()'>
  <span *ngIf='!checked'>⚪️</span>
  <span *ngIf='checked'>

总结

以上是小编为你收集整理的Angular 5.将类导入到彼此时,在typescript中的循环依赖全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

我们今天的关于Angular 2,TypeScript中的sprintfangularjs typescript的分享已经告一段落,感谢您的关注,如果您想了解更多关于Angular 12 更新 - Angular 编译器需要 TypeScript、Angular 2 typescript调用javascript函数、Angular 2 – typescript中的window.print是什么、Angular 5.将类导入到彼此时,在typescript中的循环依赖的相关信息,请在本站查询。

本文标签: