在TypeScript中,const
关键字不能用于声明类属性.这样做会导致编译器出错,"类成员不能拥有'const'关键字."
我发现自己需要在代码中清楚地指出不应该更改属性.如果我在声明属性后尝试为该属性分配新值,我希望IDE或编译器出错.你们是如何实现这一目标的?
我目前正在使用只读属性,但我是Typescript(和JavaScript)的新手,并想知道是否有更好的方法:
get MY_CONSTANT():number {return 10};
Run Code Online (Sandbox Code Playgroud)
我正在使用typescript 1.8.建议?
PS:我现在正在使用打字稿2.0.3,所以我接受了大卫的回答
我正在尝试这段代码
$json = file_get_contents("http://www.google.com/alerts/preview?q=test&t=7&f=1&l=0&e");
print_r(json_decode(utf8_encode($json), true));
//////////////
// Define the errors.
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
if (!strncmp($name, "JSON_ERROR_", 11)) {
$json_errors[$value] = $name;
}
}
// Show the errors for different depths.
foreach (range(4, 3, -1) as $depth) {
var_dump(json_decode($json, true, $depth));
echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多函数,html_entities_decode,utf8_encode和解码,解码十六进制代码,但我总是得到错误"JSON_ERROR_UTF8".
我该怎么解决这个问题?
Angular 4.2
同 Typescript 2.3
我正在重构一个服务,负责创建一个新的脚本标记并将其添加到文档中.
这是旧代码:
loadScript(src:string){
const script = document.createElement('script');
document.body.appendChild(script);
script.src = src;
}
Run Code Online (Sandbox Code Playgroud)
现在,我想使用它Renderer2
来避免直接进行DOM操作.所以我在服务中注入了我需要的内容并更新了代码:
constructor(private renderer:Renderer2, @Inject(DOCUMENT) private document){}
loadScript(src:string){
const script = this.renderer.createElement('script');
this.renderer.appendChild(this.document.body,script);
script.src = src;
}
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了这个错误:
错误:没有Renderer2的提供者!
该服务属于a,CoreModule
其唯一导入CommonModule
来自@angular/common
这个plunkr证明了这个问题
我正在尝试测试Angular应用程序中主机组件和子组件之间的交互.我不知道如何获取对父元素创建时创建的子组件的引用.这是设置:
child.component.spec.ts
@Component({template: `<child [data]="model"></child>`})
class HostComponent {
public model:any;
}
describe('ChildComponent', () => {
let hostFixture: ComponentFixture<HostComponent>;
let childFixture: ComponentFixture<ChildComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ChildComponent, HostComponent]
});
}));
beforeEach(() => {
// this creates the child component as well, but how to get to it?
hostFixture = TestBed.createComponent(HostComponent);
// this creates a NEW instance of child component, not the one from the Host template
// so it's not the instance I actually want to test
childFixture = TestBed.createComponent(ChildComponent); …
Run Code Online (Sandbox Code Playgroud) Angular 2 rc 6
,typescript 2
,node 4.5.0
,npm 2.15.9
上Windows 7
我正试图从即时编译转变为提前编译,我依赖这些资源:
我知道我需要运行编译器ngc
来生成ngfactory.ts
文件,我需要更改main.ts
为使用platformBrowser
而不是platformBrowserDynamic
引导程序.我遇到了障碍,但不知道如何继续.
1.我已确认使用即时编译可以正常运行App.我main.ts
是:
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
Run Code Online (Sandbox Code Playgroud)
2.我清除了生产文件夹中的所有应用程序文件,但保留了第三方库(例如:Angular 2,Angular 2 Material)
3.我运行"node_modules/.bin/ngc" -p ./
它运行时没有输出到控制台.我看到了.ngfactory.ts
每个.ts
组件和模块的文件.我还看到了.css.shim.ts
每个.css
持有组件样式的文件.此外,.js
和.js.map
文件已被transpiled,并放置在生产目录
4.如果我此时尝试运行应用程序,我会看到包含组件模板的404 not found
所有.html
文件的错误
5.我手动将所有模板文件(.html
)移动到生产目录并运行应用程序.它运行正常,但它仍然使用即时编译(255个请求,包括compiler.umd.js
)
6.我改变main.ts
为: …
我在httpd-vhosts.conf中设置了一个环境变量
SetEnv EARLY_VAR 1
Run Code Online (Sandbox Code Playgroud)
我尝试根据它在.htaccess中的值设置特殊规则
<If "%{ENV:EARLY_VAR} == '1'">
SetEnv TEST_VAR if_branch
</If>
<Else>
SetEnv TEST_VAR else_branch
</Else>
Run Code Online (Sandbox Code Playgroud)
我希望TEST_VAR
环境变量相等if_branch
.在PHP中
var_dump(getenv('EARLY_VAR')); // string '1'
var_dump(getenv('TEST_VAR')); // string 'else_branch'
Run Code Online (Sandbox Code Playgroud)
我也尝试EARLY_VAR
在.htaccess
If/Else上面设置,使用SetEnv
和SetEnvIf
.始终执行Else分支.
为什么是这样?
Apache 2.4
我正在尝试在我的Angular 4
项目中使用自定义构建的管道.无法弄清楚为什么编译器无法在组件模板中识别我的管道.
QFormat管道
@Pipe({ name: 'qFormat'})
export class QFormatPipe implements PipeTransform {...}
Run Code Online (Sandbox Code Playgroud)
SharedModule
@NgModule({
declarations: [QFormatPipe],
exports: [QFormatPipe]
})
export class SharedModule { }
Run Code Online (Sandbox Code Playgroud)
FeatureModule
@NgModule({
imports: [SharedModule],
declarations: [MyComponent]
})
export class FeatureModule{ }
Run Code Online (Sandbox Code Playgroud)
MyComponent.html
<div>{{someProp | qformat}}</div>
^^^^^^^
Run Code Online (Sandbox Code Playgroud)
模板抛出错误:
Angular:无法找到管道'qformat'
我错过了什么吗?
我很难用键入我的Map
对象typescript 1.8.10
.以下是core-js
定义Map接口的摘录:
interface Map<K, V> {
clear(): void;
delete(key: K): boolean;
forEach(callbackfn: (value: V, index: K, map: Map<K, V>) => void, thisArg?: any): void;
get(key: K): V;
has(key: K): boolean;
set(key: K, value?: V): Map<K, V>;
size: number;
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个使用字符串键的地图,并且只存储具有该形状的值{name:string,price:number}
.我尝试用以下方式声明我的对象:
let oMap:Map<string,{name:string,price:number}> = new Map();
Run Code Online (Sandbox Code Playgroud)
但是,编译器会抛出错误TS2322: Type 'Map<{}, {}>' is not assignable to type 'Map<string, { name: string; price: number; }>'
.Map
在打字稿中使用ES6 对象时,有没有办法利用强类型?
我正在尝试编写一个DataResolver
服务,允许Angular 2路由器在初始化组件之前预加载数据.
解析器需要调用不同的API端点来获取适合于正在加载的路由的数据.我正在构建一个通用解析器,而不是为每个组件设置一个解析器.因此,我想在路由定义中传递指向正确端点的自定义输入.例如,考虑以下路线:
app.routes.ts
appRoutes = [
{
path: 'project/:id',
component: ProjectComponent,
resolve: { data: DataResolver }
},
{
path: 'store/:id',
component: StoreComponent,
resolve: { data: DataResolver }
}
]
Run Code Online (Sandbox Code Playgroud)
首先,解析器需要调用/path/to/resource1/id
.在第二种情况下,/path/to/resource2/id
.理想情况下,我会将端点作为路径定义中的参数传递,然后在DataResolver
构造函数中可用.
这可以用通用的解析器类来完成吗?
Angular 2.0.1
写的 Typescript 2.0.3
在Angular 5中,如果我拥有AbstractClassService
并ExtendedClassService
扩展了抽象,我可以在我的NgModule的providers数组中执行此操作:
@NgModule({
providers: [
{provide: AbstractClassService, useClass: ExtendedClassService}
]
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
这将允许我切换ExtendedClassService
到另一个进行测试或任何非常容易的.这仍然可以使用Angular 6完成,但是providedIn
可以在服务本身中设置新选项以减少包大小:
@Injectable({providedIn: 'root'})
export class ExtendedClassService extends AbstractClassService {}
Run Code Online (Sandbox Code Playgroud)
有没有办法让我在使用新的时候完成与Angular 5相同的操作providedIn
?像这样的东西:
@Injectable({providedIn: 'root', provide: AbstractClassService})
export class ExtendedClassService extends AbstractClassService {}
Run Code Online (Sandbox Code Playgroud) angular ×6
typescript ×5
.htaccess ×1
angular2-aot ×1
apache ×1
apache2.4 ×1
deployment ×1
ecmascript-6 ×1
javascript ×1
json ×1
jsonp ×1
npm ×1
parsing ×1
php ×1
unit-testing ×1