我需要在我的 API 上发布一些东西。我有这个功能可以正常工作:
TradeContainer.js:
callApi(action){
var actionInfo = {
user_id: this.props.currentUser.id,
action: action
}
fetch('http://localhost:3000/actions', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(actionInfo)
})
.then(res => res.json())
.then(data => console.log(data))
}
Run Code Online (Sandbox Code Playgroud)
我想将 fetch() 移动到我进行所有 API 调用的另一个文件。在那个文件中,我已经有几个 fetch 函数(使用 get 方法)并且它们工作正常。但是,当我使用 post 方法将此 fetch() 移动到该文件时,出现错误:
类型错误:无法读取未定义的属性“then”
我的代码:
TradeContainer.js:
import { saveAction } from '../components/apiCalls.js'
callApi(action){
var actionInfo = {
user_id: this.props.currentUser.id,
action: action
}
//this is fetch() function that i moved to apiCalls.js file.
//And this two …Run Code Online (Sandbox Code Playgroud) 我有菜单。每个菜单项都有 SVG 图像和文本。SVG 嵌入使用<object>
<ul id="menu-content" class="menu-content collapse out">
<li id="calculator">
<a href="#">
<tr>
<td>
<object type="image/svg+xml" data="assets/calculator.svg">
</object>
</td>
<td class="menu-option">
<span class="menu-option">
Pricing & Services
</span>
</td>
</tr>
</a>
</li>
.....
</ul>
Run Code Online (Sandbox Code Playgroud)
当我将鼠标悬停在菜单项上时,此项的背景颜色会发生变化。但我还需要更改 SVG 颜色。现在我知道当你完全悬停在它上面时如何改变 SVG 颜色。但是当我悬停在父元素上时该怎么办。
用户数组可以具有多个元素(用户)。在模板中,我遍历此数组并为每个元素添加一个表单。现在,每个表单都绑定到相同的表单组值。如果我为第一个用户填写表格,则所有表格的提交按钮都将启用。如何将每个表单指向唯一的formGroup或唯一的变量?
零件:
import { FormControl, FormGroup, Validators, FormBuilder} from '@angular/forms';
...
export class UserComponent implements OnInit {
form: FormGroup;
@input users: any[];
constructor(private _fb: FormBuilder) {}
ngOnInit() {
this.form = this._fb.group({
address: new FormControl('', [Validators.required]),
phone: new FormControl('', [Validators.required]),
});
}
add() {
if (this.form.valid){
// code
}
}
Run Code Online (Sandbox Code Playgroud)
模板:
<div *ngFor="let user if users">
{{user.name}}
<form [formGroup]="form" (ngSubmit)="add()">
<div class="form-group">
<label>Address:</label>
<input formControlName="address" class="form-control">
<label>Phone:</label>
<input formControlName="phone" class="form-control"></input>
</div>
<button type="submit" class="submit" [disabled]="!form.valid">Submit</button>
</form>
</div>
Run Code Online (Sandbox Code Playgroud) 我在 StackOverflow 上只找到了一个解决方案,但用户说它对他们不起作用。
所以,我将 SVG 图像嵌入到 html 中:
<object id="help-center" type="image/svg+xml" data="assets/help_center.svg">
</object>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我不明白如何在悬停时更改颜色。
在这个片段https://jsfiddle.net/annaolsh/3j8dmnn2/ 中, SVG 就在 HTML 中,并且在悬停时颜色更改成功。如何做同样的事情?
我正在编写第一个测试并处理此错误:
错误:类型 SearchComponent 是 2 个模块声明的一部分:AppModule 和 DynamicTestModule!请考虑将 SearchComponent 移至导入 AppModule 和 DynamicTestModule 的更高模块。您还可以创建一个新的 NgModule 来导出并包含 SearchComponent,然后在 AppModule 和 DynamicTestModule 中导入该 NgModule。
在我的应用程序中,我只有一个模块:AppModule.ts. 并且SearchComponent只在那里声明。如果我创建第二个模块(又名 ChildModule)并将 SearchComponent 移动到那里,错误消息将具有相同的内容,但模块的名称将从 AppModule 更改为 ChildModule。我在我拥有的每个组件中都有相同的错误。
如果我从规范文件中的导入中删除 AppModule 并添加NO_ERRORS_SCHEMA,错误就会消失。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { OtherComponentOne} from …Run Code Online (Sandbox Code Playgroud) 我没有找到类似问题的公认答案,因此请不要将其标记为重复。我有一个“请求摘要”组件和“搜索”组件。“搜索”组件在“请求摘要”组件中呈现为弹出窗口。我需要单击搜索组件中的匹配结果并被重定向到新的“请求摘要”页面。'request-summary' 订阅了 URL 更改并成功查看它们,但不会重新加载自身。如何实现?
@Component({
selector: 'request-summary',
templateUrl: './request-summary.html',
styleUrls: ['./request-summary.css'],
encapsulation: ViewEncapsulation.None
})
export class RequestSummaryComponent {
private subscription: Subscription;
private requestId:number;
constructor(private documentService: DocumentService,private router:
Router,private activatedRoute: ActivatedRoute,private requestDist:
RequestDistService,private eRef: ElementRef){
}
ngOnInit() {
this.activatedRoute.params.subscribe(params => {
this.requestId = params['requestId']
console.log("URL id has changed")
});
this.getRequestInfo(); //some logic for rendering selected request
info
}
Run Code Online (Sandbox Code Playgroud)
搜索组件:
@Component({
selector: 'search',
templateUrl: './search.html',
styleUrls: ['./search.css'],
})
export class SearchComponent {
public userInput = '';
private data = []
constructor(private requestDist …Run Code Online (Sandbox Code Playgroud) 我有2个输入字段:名称和姓氏.我有2个按钮:提交和'添加一个人'.单击"添加人"应添加一组新字段(名称和姓氏).怎么实现呢?我找到了如何动态添加单个输入字段的解决方案,但在这里我需要添加一个集合
我的代码现在没有"添加人"功能:
import { FormControl, FormGroup, Validators } from '@angular/forms';
export class AppComponent implements OnInit {
form: FormGroup;
constructor(){}
ngOnInit(){
this.form = new FormGroup({
name: new FormControl('', [Validators.required, Validators.minLength(2)]),
lname: new FormControl('', [Validators.required, Validators.minLength(2)])
});
}
....
}
Run Code Online (Sandbox Code Playgroud)
模板:
<form [formGroup]="form" (ngSubmit)="submit()">
Name: <input type="text" formControlName="name">
Last Name: <input type="text" formControlName="lname">
<button type="button">Add a Person</button>
<button type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud) angular ×4
javascript ×3
css ×2
html ×2
svg ×2
es6-promise ×1
jasmine ×1
jquery ×1
promise ×1
reactjs ×1
testing ×1
unit-testing ×1