angular - 域模型不起作用 - Angular 6 & core.net 2 REST with Webpack
问题描述
我有以下问题...
环境: IDE:VS2017 项目:Angular 6 和 .Net core 2 配置了 webpack 开发系统:Win-Server 2012 R2 测试:VS2017 使用 IIS Express 和 Chrome 的调试模式
版本和配置:
包.json:
- 脚本内配置:“build”:“webpack --config webpack.config.js --progress --profile --mode=development”,
- "@angular/cdk": "^6.3.3",
- "@angular/cli": "^6.0.8",
- "@angular/core": "^6.0.9",
- "@angular/forms": "^6.0.9",
- "@angular/http": "^6.0.9",
- "@angular/platform-browser": "^6.0.9",
- "@angular/platform-browser-dynamic": "^6.0.9",
- "@angular/路由器": "^6.0.9",
- "@auth0/angular-jwt": "^2.0.0",
- "@ngtools/webpack": "^6.0.8",
- "@types/alertify": "^0.3.30",
- "alertify-js": "^0.3.12",
- "angular2-text-mask": "^9.0.0",
- "aspnet-prerendering": "^3.0.1",
- “引导程序”:“^4.1.1”,
- "core-js": "^2.4.1",
- "jquery": "^3.3.1",
- “jQuery 验证”:“^1.17.0”,
- "jquery-validation-unobtrusive": "^3.2.10",
- “时刻”:“^2.22.2”,
- "ng-pick-datetime": "^6.0.10",
- "ng2-charts": "^1.6.0",
- "ngx-bootstrap": "^3.0.1",
- "popper.js": "^1.14.3",
- “反射元数据”:“^0.1.8”,
- "rxjs": "^6.2.1",
- “systemjs”:“0.19.40”,
- “文本掩码插件”:“^3.7.2”,
- "webpack-cli": "^3.0.8",
- "webpack 合并": "^4.1.3",
- “zone.js”:“^0.8.26”
- "@angular/animations": "^6.0.9",
- "@angular/common": "^6.0.9",
- "@angular/compiler": "^6.0.9",
- "@angular/compiler-cli": "^6.0.9",
- "@angular/platform-server": "^6.0.9",
- "@types/jasmine": "^2.5.36",
- "@types/jquery": "^3.3.4",
- "@types/node": "^10.5.2",
- "angular2-router-loader": "^0.3.5",
- "angular2-template-loader": "^0.6.2",
- “真棒打字稿加载器”:“^5.2.0”,
- “德尔”:“最新”,
- "吞咽": "^3.9.1",
- "html-loader": "^0.5.5",
- “路径”:“最新”,
- “打字稿”:“^2.7.2”,
- "webpack": "^4.16.0",
- “webpack-dev-server”:“^3.1.4”
webpack.config.js:
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
module.exports = (env) => {
// Configuration in common to both client-side and server-side bundles
const isDevBuild = !(env && env.prod);
const sharedConfig = {
stats: { modules: false },
context: __dirname,
resolve: { extensions: [ '.js', '.ts' ] },
output: {
filename: '[name].js',
publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{ test: /\.ts$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-loader'] : '@ngtools/webpack' },
{ test: /\.html$/, use: 'html-loader?minimize=false' },
{ test: /\.css$/, use: [ 'to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
{ test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
]
},
plugins: [new CheckerPlugin()/*, new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})*/]
};
// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig, {
entry: { 'main-client': './app/boot.browser.ts' },
output: { path: path.join(__dirname, clientBundleOutputDir) },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [
// Plugins that apply in development builds only
new webpack.SourceMapDevToolPlugin({
filename: '[file].map', // Remove this line if you prefer inline source maps
moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
})
] : [
// Plugins that apply in production builds only
new webpack.optimize.UglifyJsPlugin(),
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, './app/app.module#AppModule'),
exclude: ['./**/*.server.ts']
})
])
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
const serverBundleConfig = merge(sharedConfig, {
resolve: { mainFields: ['main'] },
entry: { 'main-server': './app/boot.server.ts' },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./app/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
].concat(isDevBuild ? [] : [
// Plugins that apply in production builds only
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, './app/app.module#AppModule'),
exclude: ['./**/*.browser.ts']
})
]),
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './app/dist')
},
target: 'node',
devtool: 'inline-source-map'
});
return [clientBundleConfig, serverBundleConfig];
};
相关代码(片段):
我的服务方法:
getTimeTracking(userId: string, year: number, month: number): Promise<TimeTracking> {
const url = `${this.timeTrackingsUrl}/${userId}/${year}/${month}`;
return this.clientHttp.get<TimeTracking>(url)
.toPromise<TimeTracking>()
.catch(this.handleError.bind(this));
}
领域模型类(这里:TimeTracking):
export class TimeTracking implements Serializable<TimeTracking> {
id: string = '';
userId: string = '';
month: Date = new Date(0);
timeTrackingEntries: TimeTrackingEntry[] = [];
status: TimeTrackingStatus = 0;
[... more code ...]
isReadOnly(): boolean {
return (this.status == TimeTrackingStatus.Saved);
}
[... more code ...]
}
我的组件:
@Component({
moduleId: module.id.toString(),
templateUrl: 'time-tracking.component.html'})
export class TimeTrackingComponent implements OnInit {
timeTracking: TimeTracking | undefined;
[... more code ...]
this.timeTrackingService.getTimeTracking(this.currentUser.userid, this.selectedMonthDate.getFullYear(), this.selectedMonthDate.getMonth() + 1)
.then(timeTracking => {
if (this.timeTrackingForm) {
this.timeTrackingForm.reset();
}
this.timeTracking = timeTracking;
})
[... more code ...]
}
这里是我的这个组件的 html:
<button type="submit" class="btn btn-primary" [disabled]="!timeTrackingForm.form.valid || (timeTracking?.isReadOnly())" (click)="save()">Save</button>
我的问题:
一切都会编译,webpack 构建也没有问题。当我开始使用 VS2017 - IIS Express 进行调试时,将启动服务器并打开 Chrome。我导航到相关页面并通过网络概述可以说 API 调用成功并且数据已传输。但我在开发人员选项的 Chrome 控制台中收到此错误:
错误类型错误:_co.timeTracking.isReadOnly 不是函数
如果我设置断点,我确定数据对象未映射到我的域模型对象中:
问题:
- 为什么我的数据没有用“get()”映射到指定的域模型对象?
- 我有什么问题吗?
- 如何实现使用我的域模型(其中数据对象具有函数)?
谢谢你的帮助...
解决方案
推荐阅读
- c++ - 有没有办法在 C++ 编译时限制数据大小并产生编译错误?
- java - Autowire 不适用于原型 bean 中的 bean
- mysql - 在 WHERE 子句中使用一个 Count() 运算符时出现 GROUP BY 错误
- excel - 如何在 VBA 中编写克隆 SUMIF 函数?
- javascript - 如何从 auth-module、Nuxt.js 获取 access_token
- python-3.x - 有3个数组。我需要制作 3d 表面。如何解决?
- python - /i18n/setlang/ HTTP/1.1 302 set_language 不起作用
- c# - 如何让十字准线移动到枪指向的地方
- python - 为什么在引入 pipe() 时 Python 不并行计算?
- php - WooCommerce 可变产品变体中的附加价格字段