2017-02-05 11 views
1

WebPackとAngular2の新機能です。私は例を再現しようとしている、次のコードWebPack + Angular2 + Binding =エラー

<img src="assets/demo/images/car/{{car.brand}}.gif" /> 

が含まれている実行しているときのWebPACK --config webpack.config.js私は次のエラー

Module not found: Error: Cannot resolve 'file' or 'directory' ./assets/demo/images/car/{{car.brand}}.gif in C:\Users\User\Documents\Dev\1\ClientApp\app\demo\view 
@ ./ClientApp/app/demo/view/sampledemo.html 1:9835-9888 1:11867-11920 1:15789-15842 

を取得し、私はWebPACKのがしようとしていることを理解しますAngular2は/{{car.brand}}.gifというバインディングを使用しているため、既存のファイルを取得しないでください。しかし、それを修正する方法は?

enter image description here

WebPACKのコンフィグ

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]; 
+0

このhtmlはコンポーネント内にありますか? –

+0

はい、スクリーンショットを追加 – user45245

答えて

1

これは本当にWebPACKの問題ではありません。あなたのケースでは

<img [src]="getUrl()" /> 

それは取っていた:関数にsrcをバインドするためにあなたのhtml使用[]

getUrl(){ 
return "assets/demo/images/car/"+ this.car.brand+".gif"; 
} 

:あなたのコンポーネントクラス

は、URLを返すメソッドを持っています{{car.brand}}は単なる文字列であり、パスの一部です。

+0

WebPackでエラーが発生する理由「モジュールが見つかりません:エラー: 'ファイル'または 'ディレクトリ'を解決できません」と修正する方法 – user45245

+0

まだエラーが表示されていますか? –

+0

'[]'とバインドせずに文字列リテラルを 'car.brand'という値ではなく、明らかに意図したパスではなく、ファイルが存在しません。 –

関連する問題