Angular4:Component.name不适用于生产 [英] Angular4: Component.name doesn't work on production

查看:124
本文介绍了Angular4:Component.name不适用于生产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直面临这个奇怪的问题,但我不确定这是不是一个错误,或者我错过了这里的东西。

所以我有一个名为的组件TestComponent 并且在AppComponent中,当我点击它时我有一个按钮,我通过执行此 TestComponent获得 TestComponent 的名称。姓名


AppComponent

So I've been facing this weird issue but I'm not sure if it's a bug or it's me missing something here.
So I have a component called TestComponent and in the AppComponent I have a button when I click on it I get the name of the TestComponent by doing this TestComponent.name.

AppComponent:

import { TestComponent } from './test/test.component';
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: '<button (click)="showName()">Show</button>'
})
export class AppComponent {    
  showName(){
    console.log('component name: "' + TestComponent.name + '"');
  }
}

如你所见,点击我想要的按钮获取TestComponent的名称基本上是TestComponent

问题是这在开发模式下运行良好,如果我调用 ng build 也可以正常工作这是我在控制台中得到的:





当我打电话给 ng build --prod ,为了压缩文件并减小它们的大小,我得到了这样的结果:


As you can see by clicking on the button I want to get the TestComponent's name which basically "TestComponent"
The problem is that this works well on development mode and if I call ng build too works fine and this is what I get in the console:



And when I call ng build --prod, to compress files and reduce their size, I get this result:

这是正常的吗?!

推荐答案

功能 name 包含实际的函数名称。如果将函数缩小为单个字母的名称,则会丢失其原始名称。在某些点(即每个客户端应用程序)可以可能缩小的应用程序中永远不应该依赖它。 Node.js可能有例外。

Function name contains actual function name. If the function is minified to one-letter name, it loses its original name. It should never be relied on in applications that can possibly be minified at some point i.e. every client-side application. There may be exceptions for Node.js.

name 在某些浏览器中是只读的,因此可以'被覆盖。如果一个类需要标识符,则应该使用不同的名称明确指定:

name is read-only in some browsers and and thus can't be overwritten. If a class needs identifier, it should be specified explicitly under different name:

class Foo {
  static id = 'Foo';
  ...
}

这是相关问题

这篇关于Angular4:Component.name不适用于生产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆