Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular &引用;“时刻”;没有导出的成员';默认值';_Angular_Typescript_Momentjs_Angular7_Angular Moment - Fatal编程技术网

Angular &引用;“时刻”;没有导出的成员';默认值';

Angular &引用;“时刻”;没有导出的成员';默认值';,angular,typescript,momentjs,angular7,angular-moment,Angular,Typescript,Momentjs,Angular7,Angular Moment,我正在使用moment.js更改应用程序的本地日期格式,但出现以下错误: 导入库时,“矩”没有导出的成员“默认值” 下面是我的代码: import {Inject, Injectable, Optional} from '@angular/core'; import {DateAdapter, MAT_DATE_LOCALE, MatDateFormats} from '@angular/material'; import * as _moment from 'moment'; import {

我正在使用
moment.js
更改应用程序的本地日期格式,但出现以下错误:

导入库时,“矩”没有导出的成员“默认值”

下面是我的代码:

import {Inject, Injectable, Optional} from '@angular/core';
import {DateAdapter, MAT_DATE_LOCALE, MatDateFormats} from '@angular/material';
import * as _moment from 'moment';
import {default as _rollupMoment, Moment} from 'moment';

const moment = _rollupMoment || _moment;

您似乎无法导入时刻

如你所见, 对于Typescript 2.x请尝试在
tsconfig.json
文件中的
编译器选项中添加
“node”
,然后使用以下任何语法:

import * as moment from 'moment';
import moment = require('moment');
注意:确保您已使用
npm
安装
moment.js

npm install --save moment

尝试将
“allowSyntheticDefaultImports”:true添加到“compilerOptions”下的
tsconfig.json
下面是此问题的完整解决方案:

第一:

 npm install moment --save
 npm install @angular/material-moment-adapter
第二:

在依赖项下的package.json中,确保您具有以下代码:

    "@angular/material-moment-adapter": "^8.2.3",
第三:

在编译器选项下的tsconfig.json文件中添加以下内容:

"allowSyntheticDefaultImports": true
第四:

使用以下代码修改组件:

import { MomentDateAdapter } from '@angular/material-moment-adapter';
import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE } from '@angular/material/core';
import * as _moment from 'moment';
import { default as _rollupMoment } from 'moment';

const moment = _rollupMoment || _moment;
export const MY_FORMATS = {
parse: {
  dateInput: 'LL',
},
display: {
  dateInput: 'YYYY-MM-DD',
  monthYearLabel: 'YYYY',
  dateA11yLabel: 'LL',
  monthYearA11yLabel: 'YYYY',
 },
};

@Component({
 selector: 'app-newsfeedform',
 templateUrl: './newsfeedform.component.html',
 styleUrls: ['./newsfeedform.component.css'],
 providers: [
  { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },

  { provide: MAT_DATE_FORMATS, useValue: MY_FORMATS },
],
})
第五:

在component.html文件中,日期选择器类似于下面的代码:

     <mat-form-field appearance="outline" fxFlex>
                    <input matInput [matDatepicker]="publishdate" placeholder="Publish Date"
                        formControlName="publishdate">
                    <mat-datepicker-toggle matSuffix [for]="publishdate"></mat-datepicker-toggle>
                    <mat-datepicker #publishdate></mat-datepicker>
                </mat-form-field>


也就是说,您可以自定义Angular material datepicker的任何格式,只需修改上面给出的格式。

gulpfile.js的外部部分添加
'moment/moment'
'moment'
两者

externals: [
      'moment/moment',
       'moment'
   ]

tsconfig.json
文件中添加以下行:

"allowSyntheticDefaultImports": true

请参阅moment的版本?@dileepkumarjami“moment”:“^2.24.0”是否尝试添加上述内容?请尝试在尝试@dileepkumarjami的建议后重新运行您的项目,因为在更改Tconfig时这是必要的。jsonI对单词“default”有问题。添加Tconfig是的,但在Tconfig的哪个部分?根?编译器选项?lib?在
编译器选项
部分中,添加到
tsconfig.app.json
有效,而不是
tsconfig.json