2017-02-05 4 views
1

WebPack 및 Angular2의 새로운 기능입니다. 다음 코드WebPack + Angular2 + Binding = 오류

<img src="assets/demo/images/car/{{car.brand}}.gif" /> 
들어있는 예를 재현하기 위해 노력하고있어

실행하는 경우 웹팩 --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 

나는 웹팩가 노력하고 이해 Angular2가/{{car.brand}}.gif 바인딩을 사용하기 때문에 기존 파일을 가져 오지 마십시오. 그러나 그것을 고치는 방법?

enter image description here

웹팩 구성

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

이 정말 웹팩 문제가되지 않습니다. 귀하의 경우에는

<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'의 값이 아니며 의도 한 경로가 아니며 파일도 존재하지 않습니다. –

관련 문제