【问题标题】:Angular 2 - Webpack 2 - template-loader not working with templateUrlAngular 2 - Webpack 2 - 模板加载器不适用于 templateUrl
【发布时间】:2017-04-10 07:46:21
【问题描述】:

我为我的应用程序创建了一个最小的 webpack 示例,并尝试使用带有“./file.name.html”的 templateUrl 来编译。但我的应用程序显示的唯一内容是我的索引页面上的 templateUrl "./file.name.html" 的名称。应用程序似乎可以正常工作,因为控制台和

中没有错误
<app>... Loading </app> 

不再显示。在我尝试使用 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);

app.component 定义看起来像

.... Imports

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

我的 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'
             }
        ]
    }
};

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"
  }
}

和 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
   }

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

【问题讨论】:

    标签: angular angular-ui-router webpack ts-loader


    【解决方案1】:

    您需要使用template: require("./app.component.html") 而不是templateUrl:"..." 才能使其工作。

    Webpack 扫描您的文件并搜索例如需要(...)标签。

    【讨论】:

    • 来自“angular2-template-loader”github.com/TheLarkInn/angular2-template-loader#how-does-it-work 的文档说我可以链接普通的 TypeScript 加载器,并且模板加载器可以处理 m 个相对路径信息。 Webpack 的 Angular 2 文档也显示了这一点,但我不知道我错过了什么。
    • 嗯好吧,我们总是在我们的应用程序中使用 require 语句。
    • 你能提供一个 Plunker 吗?
    • 今晚我会创建一个小例子并上传。
    • angular2-template-loader 应该转换为 require 对吗?
    【解决方案2】:

    我找到了我的问题!

    不要使用像

    这样的 ES6 引号
    templateUrl: \`./app.component.html`
    

    这只会在您的应用中呈现“./app.component.html”

    使用正常的引号,如下所示:

    templateUrl: './app.component.html'
    templateUrl: "./app.component.html"
    

    【讨论】:

    • 这样做会破坏它的原因是angular2-template-loader 包使用了正则表达式。您可以查看node_modules\angular2-template-loader 并查看包含正则表达式的index.js 文件。
    • 我正在查看文档,after 语句显示 @Component({ selector: 'awesome-button', template: require('./button.template.html'), styles: [require ('./button.style.css')] })。我看到了 keep url 选项,但它没有说明相对路径。除了使用 awesome-typescript-loader 添加加载器之外,您还需要做任何额外的工作吗?
    • @Winnemucca 不需要最新的模板加载器和 webpack
    【解决方案3】:

    这不起作用的另一个原因是,如果您已经编译了与您的 .ts 文件相对应的 .js 文件。有些 IDE 会在您保存或编译时自行创建。

    这样的效果是,当请求“模块”时,它首先找到 .js 文件,并且根本不处理 .ts 文件 - 所以它不会通过这个“链”。

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

    <PropertyGroup>
      <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    </PropertyGroup>
    

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

    【讨论】:

      猜你喜欢
      • 2017-03-25
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 2016-02-18
      • 2017-06-17
      • 2016-09-20
      • 2018-05-24
      • 1970-01-01
      相关资源
      最近更新 更多