我正在更新我的应用程序以使用模块结构,当我尝试将我的管道组件添加到共享模块时,我遇到了一个奇怪的问题.从我所读到的,我已经把一切都设置得正确,所以我必须遗漏一些东西.
错误: Unhandled Promise rejection: Template parse errors: The pipe 'cmgTitleize' could not be found
我有一个BrowseModule,这个模块声明了一个ProjectCardComponent有一个使用cmgTitleize管道的模板.提供访问TitleizePipe我导入我的SharedModule.
@NgModule({
declarations: [
...,
ProjectCardComponent
],
imports: [
...,
SharedModule
],
providers: [
...
]
})
export class BrowseModule { }
Run Code Online (Sandbox Code Playgroud)
的SharedModule,进口PipesModule:
@NgModule({
declarations: [
...
],
exports: [
...
],
imports: [
...,
PipesModule
]
})
export class SharedModule { }
Run Code Online (Sandbox Code Playgroud)
PipesModule声明并导出TitelizePipe:
@NgModule({
declarations: [
... …Run Code Online (Sandbox Code Playgroud) 可以说我有一个简单的模块AppModule,其中包含许多导入,声明和提供程序。现在,我想ListComponent为位于此模块的声明列表中的组件编写测试。ListComponent本身使用的很多(但不是每次)导入AppModule。我这样做是这样的:
import { TestBed } from '@angular/core/testing';
// +same copy-pasted list of imports from `AppModule`
beforeEach(done => {
TestBed.configureTestingModule({
imports: [
// +same copy-pasted list of imports from `AppModule`
],
declarations: [
// +same copy-pasted list of declarations from `AppModule`
],
providers: [
{
provide: Http,
useClass: HttpMock,
},
{
provide: Router,
useClass: RouterMock,
}
// +same copy-pasted list of providers from `AppModule`
]
});
Run Code Online (Sandbox Code Playgroud)
它有效,但是肯定是不正确的方法。我不想复制粘贴太多。也许我可以通过一些方便的方法重用AppModule?伪代码如下:
let appModule = new AppModule();
beforeEach(done …Run Code Online (Sandbox Code Playgroud) angular-module angular-components angular angular-test angular-testing
我有一些 Angular 服务,它们具有相同的方法来解析 json 响应、处理错误等(例如,如果是 422 错误,则进行捕获)。
显然,我不希望将这些方法复制并粘贴到每个服务中,但我似乎无法找到有关应将这些代码放在哪里的任何指导。
它们不是类方法,只是每个服务中当前相同的私有方法。
举个例子:
private parseToString(jsonResponse: any){
return Object.keys(jsonResponse).reduce(
(prev, next) => prev.concat(jsonResponse[next].map(
v => next + ' ' + v).join(', ')), []).join(', ');
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以创建一个帮助模块或类似 Rails 关注的东西,你可以包含它吗?
例如说我有这个文件夹:
应用程序/服务
其中有我的各种 .ts 服务文件。
我可以创建“app/services/helpers”文件夹,然后在里面放一个文件……但是.ts文件里有什么?
例如。parser-helper.ts 可能是文件名,但里面的代码是什么样的?它应该是一个模块吗?如果是这样,我如何将其包含在服务中?
例如,这是推荐的方法吗?
app/services/helpers/parser-helper.module.ts
module parserHelperModule {
export function helperA(){}
export function helperB(){}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将旧的 angular 1.5 服务注入到我的 angular 1.5/4 混合应用程序中的 angular 4 模块中。
的MyService,因为如果我让一个市民,我也没办法与初次它使用一个静态方法
new MyService(),因为MyService预计设置了一个param,我不知道如何提供。
我的班级 (my-service.class.ts)
import {Inject, Injectable} from '@angular/core';
@Injectable()
class MyService {
constructor(
@Inject('myOldService') private myOldService: any) {}
static getMyUrl() {
console.log(this.myOldService);
// complains
// Property 'myOldService' does not exist on type 'typeof MyService
return this.myOldService.getItemUrl();
}
}
export default MyService;
Run Code Online (Sandbox Code Playgroud)
我的模块
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
...other import..
import MyService from './my-service.class';
console.log(MyService.getMyUrl())
//has error …Run Code Online (Sandbox Code Playgroud) 在我的 angular 应用程序中,我使用 angular-redux 进行应用程序状态管理。在我的主模块中,我定义了我的 redux 存储。像这样:
export class MainModule {
constructor(private ngRedux: NgRedux<MainAppState>,
private devTools: DevToolsExtension) {
let enhancers = [];
if (environment.production === false && devTools.isEnabled()) {
enhancers = [...enhancers, devTools.enhancer()];
}
this.ngRedux.configureStore(
reducer,
{} as MainAppState,
[],
enhancers);
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了新的子模块,其中包含一些组件。这些组件应该访问应用程序状态。在这些组件之一中,我通过 @select 访问以进行存储,但这不起作用。这是我访问商店的方式:
export function getLanguage(state: LanguageState) { return state.userLanguage; }
Run Code Online (Sandbox Code Playgroud)
我在我的 ChildComponent 类中有这个代码:
export class ChildComponent implements OnInit {
@select(getLanguage) savedUserLanguage$: Observable<LanguageState>;
// more code
}
Run Code Online (Sandbox Code Playgroud)
如何从子模块访问应用程序状态存储?我应该在子模块中导入什么?只为 redux store 处理创建自己的模块会更好吗?也许我忘记了什么?
我使用 Angular v4 和 @angular-redux/store v6。
我是angular的新手,我想知道是否可以加载和基于我在app.module上创建的模块及其组件,或者在哪里是最佳选择
基本上我想做这样的事情:
if(user.deparment === 'coms') {
//Use the communications.module and its components
}
Run Code Online (Sandbox Code Playgroud)
我附上了一些图片,以便你们看到应用程序的结构。如有必要,我可以添加代码而不是图片。
用户登录后,我导航到MainModule(懒惰),在那里注册MainModule组件中使用的服务。 CarService缓存 Web 服务的结果,因此,如果用户注销,我需要销毁此服务。当用户注销时,我导航到LoginModolue。
我认为导航到LoginModule,MainModule会被破坏(实际上我在MainModule 中注册它,而不是在 AppModule 中)但我注意到如果我再次登录,旧的缓存仍然存在。这是正常的吗?当我导航到LoginModule时,不应该破坏MainModule 中提供的服务吗?
我一直在阅读文档,但似乎仍然对何时应该使用指令或组件感到困惑......
另外,什么时候最好抽象组件并将它们放入模块中?
在下面的代码中,除了在第一个示例中,我们需要导入productsModule到文件中,这两种延迟加载路由的方式有什么区别?
{ path: 'products', loadchildren: () => productsModule }
Run Code Online (Sandbox Code Playgroud)
对比
{ path: 'products', loadchildren: 'app/products/products.module#ProductsModule' }
Run Code Online (Sandbox Code Playgroud) @Injectable() 装饰器中“root”中提供的服务是否仍然必须位于模块的提供者数组中?
Angular文档并没有真正给我答案,或者我不太理解它。
在我的核心文件夹中,我有一个在根目录中提供的身份验证服务。我不想将我的核心模块导入到应用程序模块中,以便使用提供的所有服务和组件。
我是否必须在模块的提供者数组中另外设置服务,或者已经使用装饰器在根级别提供该服务就足够了吗?
dependency-injection angular-services angular-module angular
angular ×10
angular-module ×10
typescript ×2
angular-test ×1
javascript ×1
lazy-loading ×1
routes ×1