在我的角度应用程序中,我有一个presentation.module包含所有组件.我创建了一个shared-material.module包含整个应用程序中使用的所有角度材料2模块.我在之前导入了它presentation.module.在我app.module,我已经导入了presentation.module.
在app.module其声明中有app.component,header.component而footer.component
这是我的app.module:
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent
],
imports: [
// Layer's modules
PresentationModule,
BusinessDelegateModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
这是我的presentation.module:
const modules = [
SharedMaterialModule,
SharedModule,
HomePresentationModule,
SecurityPresentationModule,
]
@NgModule({
imports: [...modules],
exports: [...modules]
})
export class PresentationModule {
}
Run Code Online (Sandbox Code Playgroud)
源代码shared-material.module:
// updated: Before, i have just imported …Run Code Online (Sandbox Code Playgroud) 我正在使用Angular 4和websocket创建一个聊天应用程序.为此,我遵循了这个Angular websocket教程
这是WebsocketService源代码:
import { Injectable } from '@angular/core';
import * as Rx from 'rxjs/Rx';
@Injectable()
export class WebsocketService {
constructor() { }
private subject: Rx.Subject<MessageEvent>;
public connect(url): Rx.Subject<MessageEvent> {
if (!this.subject) {
this.subject = this.create(url);
console.log("Successfully connected: " + url);
}
return this.subject;
}
private create(url): Rx.Subject<MessageEvent> {
let ws = new WebSocket(url);
let observable = Rx.Observable.create(
(obs: Rx.Observer<MessageEvent>) => {
ws.onmessage = obs.next.bind(obs);
ws.onerror = obs.error.bind(obs);
ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws);
})
let observer …Run Code Online (Sandbox Code Playgroud) 我有这个AuthGuard:
export class AuthGuard implements CanActivate {
constructor (private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (localStorage.getItem('token')) {
return true;
}
this.router.navigate(['/login']);
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的提供AppModule:
@NgModule({
declarations: [/*...*/],
imports: [NotificationRoutingModule],
providers: [AuthService, AuthGuard],
bootstrap: [AppComponent]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)
我试图在我的路由模块中使用这个后卫,如下所示:
const routes = [
{
path: 'notification',
component: NotificationRootComponent
children: [
{path: '', redirectTo: 'list', pathMatch: 'full'},
{path: 'list', component: NotificationListComponent, canActivate: [AuthGuard]},
{path: ':id', …Run Code Online (Sandbox Code Playgroud) 我有一个间隔[0; max],我想将它分成特定数量的子间隔.为此,我编写了一个函数,名为getIntervalls(max, nbIntervals)where max我是第一个区间中的最大元素,nbIntervals是预期的子区间数.
例如:
getIntervalls(3, 2)应该回来[[0,1], [2,3]],getIntervalls(6, 2)应该回来[[0,3], [4,6]],getIntervalls(8, 3)应该回来[[0,2], [3,5], [6,8]],getIntervalls(9, 3)应该回来[[0,3], [4,7], [8,9]],这是我的功能:
function getIntervalls(max, nbIntervalls) {
var size = Math.ceil(max / nbIntervalls);
var result = [];
if (size > 1) {
for (let i = 0; i < nbIntervalls; i++) {
var inf = i + i * size;
var sup = inf …Run Code Online (Sandbox Code Playgroud) 我在ES6中寻找一种优雅的方式来转换这个数组:
var src = [{x:1,y:'a'},{x:2,y:'b'}];
Run Code Online (Sandbox Code Playgroud)
到这个数组:
var desc = [[1,2],["a","b"]];
Run Code Online (Sandbox Code Playgroud)
其中包含所有属性的数组和所有值的一个数组.
为此,我写了这段代码:
var src = [{x:1,y:'a'},{x:2,y:'b'}];
var prop1 = [];
var prop2 = [];
src.forEach(item => {
prop1.push(item.x)
prop2.push(item.y);
});
var desc = [prop1, prop2];
Run Code Online (Sandbox Code Playgroud)
它工作正常,但它很长,所以我正在寻找最终的改进和短代码.
我正在尝试在 Angular 4 应用程序中使用 Angular Routes,但该应用程序无法加载与请求的路由匹配的组件:
这是app-routing.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)
app.module.ts 看起来像这样:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import …Run Code Online (Sandbox Code Playgroud) angular ×4
ecmascript-6 ×2
javascript ×2
typescript ×2
algorithm ×1
arrays ×1
canactivate ×1
module ×1
observable ×1
rxjs ×1
websocket ×1