Lui*_*mas 8 typescript angular-module angular angular6 angular7
我正在尝试实现通配符模块,我似乎没有得到它的工作:
现在我有以下代码可行:
typings.d.ts
declare module "*.json" {
const value: any;
export default value;
}
Run Code Online (Sandbox Code Playgroud)
app.component.ts
import { Component, OnInit } from '@angular/core';
import * as es from './i18n/es.json';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
hello = '-';
ngOnInit() {
this.hello = es.default.hello;
}
}
Run Code Online (Sandbox Code Playgroud)
您可能会看到一个活生生的例子在这里,但是我想实现通配符,因为看到这里(typescriptlang)和这里(SitePen接触):

实现应该允许我做这样的事情:
typings.d.ts
declare module "*.json!i18n" {
const value: any;
export default value;
}
declare module "*.json!static" {
const value: any;
export default value;
}
declare module "*!text" {
const value: any;
export default value;
}
Run Code Online (Sandbox Code Playgroud)
app.component.ts
import { Component, OnInit } from '@angular/core';
import * as es from './i18n/es.json!i18n';
import * as someObject from './static/someObject.json!static';
import * as template1 from './templates/template1.html!text';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
hello = '-';
ngOnInit() {
this.hello = es.default.hello;
console.log(someObject.default);
console.log(template1.default);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是由于某些原因,通配符未被正确识别...在运行时抛出未找到"json".
此功能工作的一个例子是在这里,当它在角2首次实施,
知道我做错了什么?
Jav*_*pse -1
根据您共享的stackblitz 代码链接,“static”和“template”目录不存在。只需创建目录并将数据放入其中即可。还要更新您的导入并从路径中删除!i18n或!static。
import * as es from './i18n/es.json';
import * as someObject from './static/someObject.json';
Run Code Online (Sandbox Code Playgroud)
我仅使用 static/someObejcts 进行了测试。模板也将以同样的方式工作。
应用程序组件.ts
import { Component, OnInit } from '@angular/core';
import * as es from './i18n/es.json';
import * as someObject from './static/someObject.json';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
hello = '-';
ngOnInit() {
this.hello = es.default.hello;
console.log(es.default.hello);
console.log(someObject.default.hello);
}
}
Run Code Online (Sandbox Code Playgroud)
打字.d.ts
declare module "*.json!i18n" {
const value: any;
export default value;
}
declare module "*.json!static" {
const value: any;
export default value;
}
Run Code Online (Sandbox Code Playgroud)
这是您的代码的工作示例
| 归档时间: |
|
| 查看次数: |
863 次 |
| 最近记录: |