Angular 2 - Webpack 2 - 模板加载器不使用templateUrl

squ*_*hel 1 angular-ui-router webpack ts-loader angular

我为我的应用程序创建了一个最小的webpack示例,我尝试使用带有"./file.name.html"的templateUrl进行编译.但我的应用程序显示的唯一内容是我的索引页面上的templateUrl"./ file.name.html"的名称.这些应用程序似乎有效,因为控制台和控制台都没有错误

<app>... Loading </app> 
Run Code Online (Sandbox Code Playgroud)

不再显示了.在我尝试使用webpack之前,我使用过systemJs,而我的应用程序运行得非常好.所以我不认为这是一个应用程序问题.

我的起始文件名是boot.ts

/// <reference path="../typings/index.d.ts" />
import 'core-js/es6';
import 'core-js/es7/reflect';
import "zone.js/dist/zone";

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
Run Code Online (Sandbox Code Playgroud)

app.component定义看起来像

.... Imports

@Component({
    selector: 'sxp-app',
    templateUrl: "./app.component.html"
})
export class AppComponent { ... } 
Run Code Online (Sandbox Code Playgroud)

我的webpack.config.js

var path = require('path');

module.exports = {
    entry: "./App/boot.ts",
    output: {
        path: path.join(__dirname, './Scripts/dist'),
        filename: "bundle.js"
    },
    resolve: {
        extensions: ['.ts', '.js']
    },
    module: {
        loaders: [
             {
                 test: /\.ts$/,
                 loader: ['awesome-typescript-loader', 'angular2-template-loader']
             },
             {
                test: /\.html$/, 
                loader: 'raw-loader'
             }
        ]
    }
};
Run Code Online (Sandbox Code Playgroud)

package.json

{
  "scripts": {
    "start": "webpack-dev-server --inline --progress --port 8080",
    "build": "rimraf dist && webpack --config config/webpack.prod.js --progress --profile --bail"
  },
  "dependencies": {
    "@angular/common": "~2.2.0",
    "@angular/compiler": "~2.2.0",
    "@angular/core": "~2.2.0",
    "@angular/forms": "~2.2.0",
    "@angular/http": "~2.2.0",
    "@angular/platform-browser": "~2.2.0",
    "@angular/platform-browser-dynamic": "~2.2.0",
    "@angularclass/hmr": "~1.2.2",
    "@angularclass/hmr-loader": "~3.0.2",
    "@angular/router": "~3.2.0",
    "@angular/upgrade": "~2.2.0",
    "core-js": "^2.4.1",
    "rxjs": "5.0.0-beta.12",
    "zone.js": "^0.6.26",
    "ui-router-ng2": "1.0.0-beta.3"
  },
  "devDependencies": {
    "@types/node": "^6.0.51",
    "@types/requirejs": "2.3.1",
    "angular2-router-loader": "^0.3.4",
    "angular2-template-loader": "^0.6.0",
    "css-loader": "0.26.0",
    "file-loader": "0.9.0",
    "html-loader": "0.4.4",
    "html-webpack-plugin": "2.24.1",
    "raw-loader": "0.5.1",
    "rimraf": "~2.5.4",
    "style-loader": "0.13.1",
    "typescript": "^2.0.10",
    "typings": "2.0.0",
    "webpack": "2.1.0-beta.27",
    "webpack-dev-server": "2.1.0-beta.12",
    "webpack-merge": "0.17.0",
    "webpack-notifier": "^1.4.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

和tsconfig.json

 {
   "compilerOptions": {
     "target": "es5",
     "module": "commonjs",
     "moduleResolution": "node",
     "sourceMap": true,
     "emitDecoratorMetadata": true,
     "experimentalDecorators": true,
     "removeComments": false,
     "noImplicitAny": false,
     "types": [
       "node" 
     ]
   },
   "awesomeTypescriptLoaderOptions": {
     "useWebpackText": true
   },
  "exclude": [
       "node_modules",
       "dist",
       "typings/main",
       "typings/index.d.ts"
     ],
     "compileOnSave": true
   }
Run Code Online (Sandbox Code Playgroud)

我正在使用Ui-router,我看到正确加载了正确的URL,但它只显示模板文件名,如"./appHeader.component.html"但没有模板.

Sim*_*ver 7

另一个不起作用的原因是你已经编译了.js文件对应的.ts文件.保存或编译时,某些IDE会自行创建这些IDE.

这样做的结果是,当请求"模块"时,它首先找到该.js文件并且根本不处理该.ts文件 - 因此它不会通过该"链".

编辑您的.csproj文件并将其添加为新的属性组(首先搜索它尚不存在).这将阻止Visual Studio .js在保存时自动生成文件.

<PropertyGroup>
  <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

确保删除所有生成的文件.js.js.map文件夹.ts下的相应文件ClientApp(组件等).首先检查源代码控制/备份以防万一.