2016-11-23 264 views
3

對於我們的項目,我們正在開發適用於角度2應用程序的mvc 5應用程序。對於捆綁,我們使用Webpack。用於混合cshtml和html的Webpack配置(角度2 + mvc)

現在的問題是,一些組件必須使用呈現cshtml視圖的mvc控制器加載html。對於其他組件,我們使用靜態HTML文件。

我想配置Webpack,以便將「靜態」組件html作爲內嵌html呈現在組件中,並且templateUrl保留用於需要動態呈現html的組件。

這是我嘗試過的許多事情之一,但我無法得到包括和排除工作。應用程序組件需要服務器呈現的cshtml。

 { 
      test: /\.ts$/, 
      loaders: ['awesome-typescript-loader', 'angular2-template-loader'], // here i want to inline the html 
      exclude: ["/client/src/app/app.component.ts"] 
     }, 
      { 
       test: /\.ts$/, 
       loaders: ['awesome-typescript-loader'], // here i want to keep the templateUrl so that the cshtml can be rendered 
       include: ["/client/src/app/app.component.ts"] 
      }, 
+0

您是否設法解決這個問題? – jkyadav

+0

@jkyadav是的我認識到包括和排除期望的正則表達式解決了這個問題。所以我用exclude排除了它:/!?app\.component.ts$/ – Jensdc

+0

謝謝,我會看看 – jkyadav

回答

2

我建議你做的是不使用'angular2-template-loader',因爲它只是增加了require()templateUrls,你可以自己做。

然後,你需要在*.cshtml頁面,提供您的主角度成分

然後爲需要得到它的從呈現MVC意見模板一套組件添加<base href="@Url.Content("~")" />這一點:

@Component({ 
    templateUrl: "MvcController/MvcActionName", 
}) 
export class AppComponent { 

對於需要靜態模板的組件,試試這個:

@Component({ 
    moduleId: module.id, 
    template: require("./app.some.component.html"), 
    styleUrls: [require("./app.some.component.css")], 
}) 
export class SomeComponent { 

靜態模板將被捆綁,其他的將從MVC動作下載。