首页 文章

用于混合cshtml和html的Webpack配置(角度2 mvc)

提问于
浏览
3

对于我们的项目,我们正在研究mvc 5应用程序,它为角度2应用程序提供服务 . 对于捆绑,我们使用Webpack .

现在问题是某些组件必须使用呈现cshtml视图的mvc控制器加载html . 对于其他组件,我们使用静态html文件 .

我想配置Webpack,以便“静态”组件html在组件中呈现为内联html,并且templateUrl保留用于需要动态呈现html的组件 .

这是我尝试过的众多事情之一,但我无法获得包含和排除工作 . app组件需要服务器呈现的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"]
             },

1 回答

  • 2

    我建议你做的是不要使用 'angular2-template-loader' ,因为它只是将 require() 添加到 templateUrls ,你可以自己做 .

    然后,您需要在 *.cshtml 页面中添加 <base href="@Url.Content("~")" /> ,为您的主要角度组件提供服务

    然后对于需要从 rendered MVC views 获取模板的组件设置:

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

    对于需要 static templates 的组件,请尝试以下操作:

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

    静态模板将被捆绑,其他模板将从MVC动作下载 .

相关问题