typescript - Angularjs 2.0 不加载 templateUrl

标签 typescript angular

我是 Angularjs-2.0 的新手。我创建了一个示例应用程序,但在加载 templateUrl 时遇到问题。

我的app.ts 文件:

import {bootstrap, Component} from 'angular2/angular2';
@Component({
    selector: 'my-app',
    templateUrl: 'grid.html'
})
class AppComponent { }
bootstrap(AppComponent);

我改变了像 ./grid.html 这样的路径,但它对我也不起作用。

有人帮我..!

最佳答案

如果你的目录结构是这样的:

|- app
   |- grid.html
   |- component.ts
   myApp.ts

然后使用templateUrl: 'app/grid.html'

更新

上面的解决方案让我们在每个地方都对 app/ 进行硬编码以包含这些文件。所以这是另一个:

  1. 在文件 tsconfig.json 中定义 "module": "commonjs"
  2. @Component 装饰器添加一行 moduleId: module.id,

于是变成了:

@Component({
    selector: 'my-app',
    moduleId: module.id,
    templateUrl: 'grid.html'
})

引用:

关于typescript - Angularjs 2.0 不加载 templateUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34224101/

相关文章:

javascript - Typescript,箭头函数和 rxjs 内的 "this"问题

reactjs - 如何使用 Preact 和 TypeScript 对组件子项进行类型检查?

Angular2 - 类型化对象的克隆数组

node.js - 在 TypeScript(也是普通的 Nodejs)中使用正则表达式解析文件名,令人惊讶的是结果为 null

typescript - 异常 : ObjectUnsubscribedError when working with Observables with RxJS and Angular2

typescript - TS2307 : Cannot find module 'ionic/ionic'

angular - 无法在递归 Angular 形式上使用 ngIf 找到带路径的控件

angular - 在 Jasmine 中模拟导入的类实例

javascript - 如何在 JavaScript/TypeScript 中创建具有非唯一键的 Map?

javascript - typescript 中的对象解构