0

I followed the "Building Single Page Applications on ASP.NET Core with JavaScriptServices" blog post. And from that I was able to create a new Angular2 template running on ASp.NET Core. That app works great and uses webpack (didn't know that was possible).

dotnet new angular    
dotnet restore    
npm install    
dotnet run

I am trying to figure out how I can add an img to an angular template and supply it a known src url - and it work. For the life of me, I cannot figure this out. Here is the 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];
};

Component

import { Component } from '@angular/core'

@Component({
    selector: 'example',
    template: `<img src='dist/myimg.png' />`
})
export class ExampleComponent { }

Update

I found this SO Q&A which helps clear up a few things. Now the issues is that I have a need to get images dynamically.

Steve answered this question pertaining to his templates for Angular2 + ASP.NET Core. I want to know how to do this dynamically, instead of statically. I want to use Angular2 binding and get an image name from the server and then bind that to the img src. Any ideas on how to do this would be appreciated. It seems like webpack requires it to be known ahead of time so that it is bundled

Community
  • 1
  • 1
David Pine
  • 23,787
  • 10
  • 79
  • 107
  • your image path looks odd. ~ will not be replaced as the application root - you are not serving an ASP.net page. – KnowHoper Feb 27 '17 at 04:57
  • After some searching [it was suggested](https://github.com/vuejs/vue-loader/issues/193#issuecomment-206510064) to use that to tell `webpack` how to handle it. If I have it has `` I get a errors stating that it cannot find the module for the image, which is crazy talk. – David Pine Feb 27 '17 at 11:50

1 Answers1

0

put image in wwwroot/dist and then...

template: "<img src='dist/myimg.png' />"
travis.js
  • 5,193
  • 1
  • 24
  • 21
  • This only works for static images, as they are then bundled via `webpack`. I want to know how to get images via **Angular2** binding that are dynamic. – David Pine Mar 01 '17 at 13:49