代码之家  ›  专栏  ›  技术社区  ›  surfspider

Visual Studio代码错误TS2305 with Moment.js示例,来自Angular Material Design教程

  •  0
  • surfspider  · 技术社区  · 6 年前

    我在 https://material.angular.io/components/datepicker/overview”choosing-a-date-implementation-and-date-format-setting中尝试了以下代码S(见下文)。 它符合并且我可以运行代码,但是我在Visual Studio代码中得到一个错误,它说“默认值不是当前导出的成员”。我使用的是Visual Studio代码1.24.1

    import default as _rollupment from'moment';
    
    
    

    src/app/component.ts(20,10)中有错误:错误ts2305:module'“”../frontend/node ou modules/moment/moment“”没有导出的成员“default”。

    import component from'@angular/core';
    从“@angular/forms”导入formcontrol;
    从“@角度/材料力矩适配器”导入“材料力矩适配器”格式
    从“@angular/material/core”导入日期适配器、mat_日期格式、mat_日期区域设置;
    
    //根据是否使用汇总,需要以不同的方式导入力矩。
    //由于moment.js没有默认导出,因此通常需要使用`*as导入`
    //语法。但是,汇总会创建一个合成的默认模块,因此我们需要使用
    //默认为语法。
    导入*作为“时刻”的“时刻”;
    //tslint:禁用下一行:无重复导入
    导入默认为从“矩”开始的“上卷矩”;
    
    const moment=上滚力矩力矩;
    
    /**@使用moment.js日期的标题日期选取器*/
    @组件({
    selector:'日期选取器力矩示例',
    templateurl:'datepicker moment example.html',
    样式URL:['datepicker-moment-example.css']
    提供程序:[
    //`通过导入,可以自动提供“momentdateadapter”和“mat-moment-date-formats”
    //` matmomentDateModule`在应用程序根模块中。我们在组件级别提供它
    //在这里,由于示例生成脚本的限制。
    提供:dateadapter,useclass:momentdateadapter,deps:[mat_date_locale],
    提供:mat_date_格式,使用值:mat_moment_date_格式,
    ,
    })
    导出类datepickerMomentExample{
    //datepicker采用“moment”对象而不是“date”对象。
    日期=新FormControl(Moment([2017,0,1]);
    }
    (见下文)。
    它符合并且我可以运行代码,但是我在Visual Studio代码中得到一个错误,它说“默认值不是当前导出的成员”。我使用的是Visual Studio代码1.24.1

    import { default as _rollupMoment} from 'moment';
    

    enter image description here

    src/app/component.ts(20,10)中有错误:错误ts2305:module'“”../frontend/node ou modules/moment/moment“”没有导出的成员“default”。

    import {Component} from '@angular/core';
    import {FormControl} from '@angular/forms';
    import {MAT_MOMENT_DATE_FORMATS, MomentDateAdapter} from '@angular/material-moment-adapter';
    import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
    
    // Depending on whether rollup is used, moment needs to be imported differently.
    // Since Moment.js doesn't have a default export, we normally need to import using the `* as`
    // syntax. However, rollup creates a synthetic default module and we thus need to import it using
    // the `default as` syntax.
    import * as _moment from 'moment';
    // tslint:disable-next-line:no-duplicate-imports
    import {default as _rollupMoment} from 'moment';
    
    const moment = _rollupMoment || _moment;
    
    /** @title Datepicker that uses Moment.js dates */
    @Component({
      selector: 'datepicker-moment-example',
      templateUrl: 'datepicker-moment-example.html',
      styleUrls: ['datepicker-moment-example.css'],
      providers: [
        // `MomentDateAdapter` and `MAT_MOMENT_DATE_FORMATS` can be automatically provided by importing
        // `MatMomentDateModule` in your applications root module. We provide it at the component level
        // here, due to limitations of our example generation script.
        {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
        {provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS},
      ],
    })
    export class DatepickerMomentExample {
      // Datepicker takes `Moment` objects instead of `Date` objects.
      date = new FormControl(moment([2017, 0, 1]));
    }
    
    4 回复  |  直到 5 年前
        2
  •  0
  •   surfspider    6 年前

    import * as _moment from 'moment';
    // import { default as _rollupMoment} from 'moment';
    // const moment = _rollupMoment || _moment;
    const moment = _moment;
    
        3
  •  0
  •   Weekend Baf    6 年前

    import * as moment from 'moment';
    type Moment = moment.Moment;
    const moment1 = moment;
    
    export class AppComponent {
    date = new FormControl(moment1());
    }