首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebPack + Angular2 +绑定=错误

WebPack + Angular2 +绑定=错误
EN

Stack Overflow用户
提问于 2017-02-05 15:14:51
回答 1查看 129关注 0票数 1

我是WebPack和Angular2的新手。我正在尝试复制这个示例,其中包含以下代码

代码语言:javascript
复制
<img src="assets/demo/images/car/{{car.brand}}.gif" />

当运行webpack -config webpack.config.js时,我得到以下错误

未找到模块:无法解析C:\Users\User\Documents\Dev\1\ClientApp\app\demo\view @./ClientApp/app/demo/view/samnanemo.html 1:9835-9888 1:11867-11920 1:15789-15842中的“文件”或“目录”。

据我了解,WebPack正在尝试get不存在文件,因为Angular2使用绑定/{car.brand}}.gif。但如何解决呢?

WebPack Config

代码语言:javascript
复制
var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var merge = require('webpack-merge');

// Configuration in common to both client-side and server-side bundles
var sharedConfig = {
    context: __dirname,
    resolve: { extensions: [ '', '.js', '.ts' ] },
    output: {
        filename: '[name].js',
        publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
    },
    module: {
        loaders: [
            { test: /\.ts$/, include: /ClientApp/, loaders: ['ts-loader?silent=true', 'angular2-template-loader'] },
            { test: /\.html$/, loader: 'html-loader?minimize=false' },
            { test: /\.css$/, loader: 'to-string-loader!css-loader' },
            { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url-loader', query: { limit: 25000 } },
            { test: /\.json$/, loader: 'json-loader' },
            { test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/, loaders: ['file-loader'] },
        ]
    }
};

// Configuration for client-side bundle suitable for running in browsers
var clientBundleOutputDir = './wwwroot/dist';
var 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.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin()
    ])
});

// Configuration for server-side (prerendering) bundle suitable for running in Node
var serverBundleConfig = merge(sharedConfig, {
    resolve: { packageMains: ['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'
});

module.exports = [clientBundleConfig, serverBundleConfig];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-05 15:23:37

这不是真正的webpack问题。

组件类中,有一个返回url的方法:

代码语言:javascript
复制
getUrl(){
return "assets/demo/images/car/"+ this.car.brand+".gif";
}

在html中,使用[]src绑定到函数:

代码语言:javascript
复制
<img [src]="getUrl()" />

在您的示例中,它只将{{car.brand}}作为字符串和路径的一部分。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42053653

复制
相关文章

相似问题

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