*通过使用以下方法解决:'declare var md:any;'导入后*
我正在建立一个网站,并在其中使用“用于仪表板的创意tim模板” 我正在使用日期和时间选择器,我面临的问题是DateTime选择器仅初始化一次,并且“在首次加载组件并加载该组件时有效,但是当我切换组件时,DateTime选择器停止工作” ,
我发现的解决方案是每次使用组件中的initialize方法加载组件时,都必须初始化DateTime选择器 但是然后我收到错误 src / app / components / booktrip / booktrip.component.ts(24,5):错误TS2304:找不到名称“ md”。
这是我的代码
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup,FormControl, Validators } from '@angular/forms';
import { Trip } from '../../mockups/trip.mockup';
@Component({
selector: 'app-book-trip',
templateUrl: './book-trip.component.html',
styleUrls: ['./book-trip.component.css']
})
export class BookTripComponent implements OnInit {
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
md.initFormExtendedDatetimepickers();
}
bookTrip(trip) {
console.log(trip);
}
}
我的angular.json是
"scripts": [
"src/assets/js/core/jquery.min.js",
"src/assets/js/core/popper.min.js",
"src/assets/js/core/bootstrap-material-design.min.js",
"src/assets/js/plugins/perfect-scrollbar.jquery.min.js",
"src/assets/js/plugins/moment.min.js",
"src/assets/js/plugins/sweetalert2.js",
"src/assets/js/plugins/jquery.validate.min.js",
"src/assets/js/plugins/jquery.bootstrap-wizard.js",
"src/assets/js/plugins/bootstrap-selectpicker.js",
"src/assets/js/plugins/bootstrap-datetimepicker.min.js",
"src/assets/js/plugins/jquery.dataTables.min.js",
"src/assets/js/plugins/bootstrap-tagsinput.js",
"src/assets/js/plugins/jasny-bootstrap.min.js",
"src/assets/js/plugins/fullcalendar.min.js",
"src/assets/js/plugins/jquery-jvectormap.js",
"src/assets/js/plugins/nouislider.min.js",
"src/assets/cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/core.js",
"src/assets/js/plugins/arrive.min.js",
"src/assets/buttons.github.io/buttons.js",
"src/assets/js/plugins/chartist.min.js",
"src/assets/js/plugins/bootstrap-notify.js",
"src/assets/js/material-dashboard.min40a0.js",
"src/assets/demo/demo.js",
"src/assets/demo/jquery.sharrre.js"
]
我得到的错误是 Error
我无法生成生产版本。
我被困了太长时间..有什么可能的解决方案吗?
答案 0 :(得分:0)
我认为这只是TypeScript错误,而md实际上是定义的-只是TypeScript不知道它-。
您是否尝试过将其直接导入使用md的文件中?
import * as md from 'material-dashboard';
您可以在此处了解更多信息:https://hackernoon.com/how-to-use-javascript-libraries-in-angular-2-apps-ff274ba601af
这就是我认为您的组件最终应该是什么样的:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup,FormControl, Validators } from '@angular/forms';
import { Trip } from '../../mockups/trip.mockup';
import * as md from 'material-dashboard';
@Component({
selector: 'app-book-trip',
templateUrl: './book-trip.component.html',
styleUrls: ['./book-trip.component.css']
})
export class BookTripComponent implements OnInit {
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
md.initFormExtendedDatetimepickers();
}
bookTrip(trip) {
console.log(trip);
}
}