我正在使用subject作为我的angular2应用程序中组件的通信服务的服务.
通话方式:
this.notification.create('test');
Run Code Online (Sandbox Code Playgroud)
服务:
export class NotificationService {
private static notificationSource = new Subject<any>()
notiCreated$ = NotificationService.notificationSource.asObservable();
create(message) {
NotificationService.notificationSource.next(message);
}
}
Run Code Online (Sandbox Code Playgroud)
被叫功能:
this.subscription = notification.notiCreated$.subscribe(
data => {
console.log(data);
this.createNotification(data, 'warning');
});
Run Code Online (Sandbox Code Playgroud)
但我想通过2个参数.我有错误.
我该怎么做?
我安装模块:
npm install --save crypto
Run Code Online (Sandbox Code Playgroud)
我将其导入到我的组件中:
import { createHmac } from "crypto";
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误:
--------------(4,28)中的错误:找不到模块“ crypto”。
我究竟做错了什么?
我注意到Facebook似乎支持使用令牌/ PIN码而非用户/登录设备登录,以便在电视或控制台等设备上使用:
https://www.facebook.com/device
在搜索开发页面时,我可以看到有一篇与此相关的文章,但至少在德国,有人给出了404.
有人有更多相关信息吗?
谢谢
我是 angular 2 的新手,我尝试了 [(ngModel)] ,如下所示。
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<input [(ngModel)]="model.name" name="name"> <h1>{{model.name}}</h1>`
})
export class AppComponent {
constructor() {
}
model = {name: "some value"};
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在浏览器中初始加载网页时产生如下所示的输出..
第二个是..
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<input [(ngModel)]="model.name" name="name"> <h1>{{model.name}}</h1>`
})
export class AppComponent {
constructor() {
}
model = {};
model.name = "some value";
}
Run Code Online (Sandbox Code Playgroud)
这个产生以下输出..
请解释两个代码示例之间的区别以及为什么它在第二个示例中不起作用。
提前致谢。
我有一个现有的Angular 2项目,我正在尝试将一些D3.js集成到我的项目中.我是Angular的新手,这将是我第一次使用D3.我将关注本教程:https://keathmilligan.net/create-reusable-chart-components-with-angular-2-and-d3-js-version-4/
我正在运行命令npm install --save d3,我得到:
??? UNMET PEER DEPENDENCY @angular/compiler@2.2.3
??? UNMET PEER DEPENDENCY @angular/compiler-cli@2.2.3
??? UNMET PEER DEPENDENCY @angular/core@2.2.3
??? d3@4.4.1
Run Code Online (Sandbox Code Playgroud)
...然后
npm WARN @ngtools/webpack@1.2.1 requires a peer of @angular/compiler@^2.3.1 but none was installed.
npm WARN @ngtools/webpack@1.2.1 requires a peer of @angular/compiler-cli@^2.3.1 but none was installed.
npm WARN @ngtools/webpack@1.2.1 requires a peer of @angular/core@^2.3.1 but none was installed.
npm WARN @ngtools/webpack@1.2.1 requires a peer of @angular/tsc-wrapped@^0.5.0 but none was installed.
npm WARN tslint-loader@2.1.5 …Run Code Online (Sandbox Code Playgroud) 我需要一个全局模态开启服务,可以从任何模块创建动态组件.我有多个模块,每个模块都有模态组件.我简化了我的项目结构:
AppModule
---ModalOpenerService
--ModalTemplate
---AModule
---ModalComponent1
---ModalComponent2
---SimpleComponent
---BModule
---ModalComponent3
---ModalComponent4
Run Code Online (Sandbox Code Playgroud)
模态开启者服务有这样的方法:
@ViewChild('modalContainer', {read: ViewContainerRef}) modalContainer;
showModal(component: Type<any>,data?:any){
let componentFactory = this.resolver.resolveComponentFactory(component);
this.modalContainer.createComponent(componentFactory );
}
Run Code Online (Sandbox Code Playgroud)
我希望我的ModalOpenerService能够创建任何模态组件并显示它.但问题是模态组件属于不同的模块,模态开启器无法在其注入器的组件工厂列表中找到它们.
No component factory found for ModalComponent1. Did you add it to @NgModule.entryComponents
Run Code Online (Sandbox Code Playgroud)
显然,ModalComponent1不是AppModule的成员.
我可以通过将工厂方法作为参数而不是像下面这样的组件类型来打开模态:
//In SimpleComponent, I call my ModalOpenerService to open modal1. Because
//the simpleComponent's parent injector (AModule's injector) can find Modal1,
//this.resolver can resolve Modal1.
showModal1(){
this.modalOpenerService.showModal(this.resolver.resolveComponentFactory(ModalComponent1));
}
Run Code Online (Sandbox Code Playgroud)
在ModalOpenerService中
@ViewChild('modalContainer', {read: ViewContainerRef}) modalContainer;
showModal(factory:ComponentFactory<any>, data?:any){
this.modalContainer.createComponent(factory);
}
Run Code Online (Sandbox Code Playgroud)
因此,模块创建自己的组件,并且能够在其entryComponents …
我有一个服务,我声明我的变量.在我的组件中,我使用此变量将数据放入其中.
服务:
@Injectable()
export class DataService {
public msgs = [];
constructor() { }
}
Run Code Online (Sandbox Code Playgroud)
现在我在我的组件中使用这个变量:
export class MessagesComponent implements OnInit {
constructor(private dataService: DataService){}
ngOnInit() {
this.getData();
}
getData(){
let msgs = [];
if (diffr <= this.geomessage[i].range) {
this.geomessage[i].dist = diffr;
msgs.push(this.geomessage[i]);
//console.log("this message: ", this.geomessage[i]); //DEBUG
}
this.dataService.msgs = msgs;
}
}
Run Code Online (Sandbox Code Playgroud)
我只发布了必要的代码.它this.dataService.msgs充满了消息,这很好.当我到另一个组件this.dataService.msgs的数据仍然存在,但是当我回来的TOT Messagescomponent的this.dataService.msgs是undefined,直到我再次填满它,但我需要在它的数据.有人知道怎么做吗?
谢谢
我试图在组件的模板中打印一个值.
但我得到了上述错误
Cannot read property 'name' of undefined
我已从此问题的代码中删除了额外的内容.我想访问详细信息的第一行.
组件文件
import {
Component
} from '@angular/core';
import {
Person
} from './person';
import {
getPersonDetailsService
} from './person.service';
@Component({
selector: 'my-app',
template: `
{{data[0].name}}
`
})
export class AppComponent {
data: Person[] = [];
ngOnInit(): void {
this.getPersonDetailsService.getData()
.then(data => this.data = data.slice(1, 5));
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用app.module.ts中的以下行导入HttpClientModule:
从'@ angular/common/http'导入{HttpClientModule};
和
imports: [
...
HttpClientModule,
...
]
Run Code Online (Sandbox Code Playgroud)
但是我一直得到同样的错误:
src/app/app.module.ts(4,38):错误TS2307:找不到模块'@ angular/common/http'.
尽管这些教程中包含了这些确切的行:
我该如何解决这个问题并使用HttpClient?
我使用的是Windows 10。在CMD中,我尝试在我的电子项目中执行以下命令:
setx GH_TOKEN "ghp_B3kYZy7OibM1Rka4Y3jLSiBUlvtSS717FhvE" npm run publish
Run Code Online (Sandbox Code Playgroud)
我得到了这个错误:
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.
Run Code Online (Sandbox Code Playgroud)
以下代码是Package.json文件的部分内容:
"scripts": {
"publish": "electron-builder build -w -p onTagOrDraft"
}
Run Code Online (Sandbox Code Playgroud)
angular ×8
typescript ×5
javascript ×3
node.js ×2
components ×1
console ×1
d3.js ×1
electron ×1
facebook ×1
github ×1
import ×1
npm-install ×1
oauth ×1
rxjs ×1
service ×1
subject ×1
television ×1
variables ×1
windows ×1