我跟踪了“用ASP.NET内核用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 { }更新
我找到了这个所以问与答,它帮助清理了一些事情。现在的问题是,我需要动态获取图像。
Steve回答了与他的Angular2 + ASP.NET Core模板相关的问题。我想知道如何动态地,而不是静态地做这件事。我希望使用Angular2绑定,并从服务器获取图像名,然后将其绑定到img src。对于如何做到这一点,我们将不胜感激。看来webpack需要提前知道,这样才能把它捆绑起来。
发布于 2017-02-27 05:25:35
把图像放到www.root/dist然后..。
template: "<img src='dist/myimg.png' />"https://stackoverflow.com/questions/42477324
复制相似问题