我已下载了ngx-translate/core软件包,并按照文档说明进行操作.
我无法让翻译工作.我做的步骤:
1]定义AppModule中的所有内容
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { TranslateModule } from '@ngx-translate/core';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { routing } from './app.routes';
import { AppComponent } from './app.component';
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
@NgModule({
declarations: [
AppComponent
],
imports: [ …Run Code Online (Sandbox Code Playgroud) 这可能是一个初学者的问题,问题与理解为什么我们需要将服务注入组件有关.
1]当我们只能创建一个静态方法时,为什么我们需要将服务注入每个组件,它将返回相同的输出,我们真的不需要继续编写额外的代码来注入这些服务?
假设我有一个类似下面的身份验证服务,具有正常约定:
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import { GlobalConfig } from "../global-config";
// Models
import { UserModel } from "../models/user-model";
@Injectable()
export class AuthenticationService {
constructor(private http: Http) { }
authenticate(user: UserModel): Observable<UserModel> {
let userObject = this.http.post(GlobalConfig.getUrlFor('authentication'), user)
.map((response: Response) => {
let responseJSON = response.json();
let userObj = <UserModel>{
UserId: responseJSON.UserId,
FirstName: responseJSON.FirstName,
LastName: responseJSON.LastName,
FullName: responseJSON.FullName,
Email: responseJSON.Email, …Run Code Online (Sandbox Code Playgroud) 所以我一直在做很多研究,但我就是无法弄清楚。
我想使用 Angular 材质表单控件制作一个 Textbox 组件
按照本教程,我已按如下方式实现
文本框.component.html
<mat-form-field>
<input matInput type="text" [placeholder]="placeholder" [(ngModel)]="value" />
<mat-error>This field is required</mat-error>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
textbox.component.ts
import { Component, Input, forwardRef, OnDestroy, ElementRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor, FormControl, NgControl } from '@angular/forms';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { MatFormFieldControl } from '@angular/material';
import { Subject } from 'rxjs';
import { FocusMonitor } from '@angular/cdk/a11y';
@Component({
selector: 'text-box',
templateUrl: './text-box.component.html',
styleUrls: ['./text-box.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() …Run Code Online (Sandbox Code Playgroud) 你好,我正在尝试从react-router-dom“typescript”导入useLocation Hook,我找不到它
根据 React Router文档,我很确定它存在。
然而在打字稿中它一直告诉我没有名为 useLocation 的导出成员
这就是我导入它的方式:
import { useLocation } from 'react-router-dom';
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
模块“../../node_modules/@types/react-router-dom”没有导出成员“useLocation”.ts(2305)