我在Angular中构建了一个基本的应用程序,但是我遇到了一个奇怪的问题,我不能将服务注入到我的一个组件中.它注入了我创建的其他三个组件中的任何一个.
首先,这是服务:
import { Injectable } from '@angular/core';
@Injectable()
export class MobileService {
screenWidth: number;
screenHeight: number;
constructor() {
this.screenWidth = window.outerWidth;
this.screenHeight = window.outerHeight;
window.addEventListener("resize", this.onWindowResize.bind(this) )
}
onWindowResize(ev: Event) {
var win = (ev.currentTarget as Window);
this.screenWidth = win.outerWidth;
this.screenHeight = win.outerHeight;
}
}
Run Code Online (Sandbox Code Playgroud)
它拒绝使用的组件:
import { Component, } from '@angular/core';
import { NgClass } from '@angular/common';
import { ROUTER_DIRECTIVES } from '@angular/router';
import {MobileService} from '../';
@Component({
moduleId: module.id,
selector: 'pm-header',
templateUrl: 'header.component.html',
styleUrls: ['header.component.css'],
directives: [ROUTER_DIRECTIVES, …Run Code Online (Sandbox Code Playgroud) 当我使用"ng build --prod"构建Angular应用程序时,我遇到了以下错误.当我使用Angular 4构建并且使用Angular 5获得错误时,这是有效的.使用Angular 5"ng serve"工作正常.
错误中的错误:无法在mypath/node_modules/time-ago-pipe/time-ago-pipe.ts中确定类TimeAgoPipe的模块!将TimeAgoPipe添加到NgModule以修复它.
获取https://www.npmjs.com/package/time-ago-pipe的错误
有解决方案吗
每次我在共享文件夹中生成内容时,都会重建index.ts文件,并按字母顺序放置导出.这似乎打破了我的依赖.手动更改顺序,以便在具有依赖项的类之前导出依赖项使其再次起作用.
如果我们有app/shared/auth.guard.ts:
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { AuthService, User } from './';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private accountService: AuthService, private router: Router) { }
canActivate(next: ActivatedRouteSnapshot): Observable<boolean> {
let result = this.accountService.currentUser.first().map(user => user != null);
let route: any[] = ['/login'];
if (next.url.length) {
route.push({ redirectUrl: next.url });
}
result.subscribe(isLoggedIn => {
if (!isLoggedIn) {
this.router.navigate(route);
}
}); …Run Code Online (Sandbox Code Playgroud) 我已多次读过Angular不推荐使用的桶,但我没有在任何地方看到它.在Angular网站上,我看到他们说他们正在使用它们.
https://angular.io/guide/glossary#B
更新:在一个明显的更新到角文档,我已经链接到页面不会出现了提万桶.如果有人可以找到他们说的地方,或者使用这些用法的例子,请用链接评论.