首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用dotnet角度webpack加载图像

无法使用dotnet角度webpack加载图像
EN

Stack Overflow用户
提问于 2017-02-27 03:22:47
回答 1查看 739关注 0票数 0

我跟踪了“用ASP.NET内核用JavaScriptServices构建单页应用程序”的博客文章。然后,我创建了一个新的Angular2模板,运行在ASp.NET Core上。该应用程序运行良好,并使用webpack (不知道这是可能的)。

代码语言:javascript
复制
dotnet new angular    
dotnet restore    
npm install    
dotnet run

我试图弄清楚如何将一个img添加到一个角模板中,并提供一个已知的src url --它可以工作。为了我的生命,我想不出这点。这是webpack.config.js

代码语言:javascript
复制
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];
};

组件

代码语言:javascript
复制
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需要提前知道,这样才能把它捆绑起来。

EN

回答 1

Stack Overflow用户

发布于 2017-02-27 05:25:35

把图像放到www.root/dist然后..。

代码语言:javascript
复制
template: "<img src='dist/myimg.png' />"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42477324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档