我遵循"使用JavaScriptServices构建单页应用程序"博客文章。由此,我能够创建一个新的 angular2 模板在 asp.net core 上运行。该应用程序效果很好并使用WebPack(不知道这是可能的)。
dotnet new angular
dotnet restore
npm install
dotnet run
我试图弄清楚如何将img
添加到角模板中并为其提供已知的src
URL,并且它起作用。对于我的一生,我无法弄清楚这一点。这是webpack.config.js
:
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
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$/, include: /ClientApp/, use: ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] },
{ test: /.html$/, use: 'html-loader?minimize=false' },
{ test: /.css$/, use: ['to-string-loader', 'css-loader'] },
{ test: /.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
]
},
plugins: [new CheckerPlugin()]
};
// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig, {
entry: { 'main-client': './ClientApp/boot-client.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()
])
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
const serverBundleConfig = merge(sharedConfig, {
resolve: { mainFields: ['main'] },
entry: { 'main-server': './ClientApp/boot-server.ts' },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./ClientApp/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
],
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
devtool: 'inline-source-map'
});
return [clientBundleConfig, serverBundleConfig];
};
组件
import { Component } from '@angular/core'
@Component({
selector: 'example',
template: `<img src='dist/myimg.png' />`
})
export class ExampleComponent { }
update
我发现了这个Q&amp; a,有助于清除一些事情。现在问题是我需要动态获取图像。
史蒂夫回答了与 Angular2 ASP.NET Core 的模板有关的这个问题。我想知道如何动态地而不是静态地做到这一点。我想使用 angular2 绑定并从服务器获取图像名称,然后将其绑定到img src
。关于如何执行此操作的任何想法将不胜感激。似乎WebPack要求提前知道它,以便捆绑
将图像放在wwwroot/dist中,然后...
template: "<img src='dist/myimg.png' />"