使用ng-upgrade将Angualr.js转换为Angular

时间:2018-06-29 09:40:02

标签: angularjs angular ng-upgrade

我有一个Angularjs应用程序。我必须将其迁移到Angular。我正在关注角度教程-https://angular.io/guide/upgrade

我的角度app.module看起来像

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UpgradeModule } from '@angular/upgrade/static';
import module from './app.module.ajs';

@NgModule({
    imports: [
        BrowserModule,
        UpgradeModule
    ]
})
export class AppModule {
    constructor(private upgrade: UpgradeModule) { }

    ngDoBootstrap(){
        this.upgrade.bootstrap(document.documentElement, [module.name], {strictDi: true});
    }
}

我正在此文件中导入我的angularjs应用,并手动对其进行引导。

我已将main.ts文件添加到引导角度模块app.module。我的主要.ts看起来像

import 'zone.js';
import 'reflect-metadata';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { setAngularLib } from '@angular/upgrade/static';
import * as angular from 'angular';
import { AppModule } from './app.module';

setAngularLib(angular);
platformBrowserDynamic().bootstrapModule(AppModule);

我在网络包中添加了main.ts作为入口点。

在我的index.html中,我删除了ng-app。我的index.html看起来像:

  <script  src="javascripts/main.js">
  <script  src="javascripts/vendor.js">
  <body>
      <main-app></main-pp>

  </body>

是angularJS组件。 main.js和vendor.js是引导程序生成的捆绑文件。

Package.json:

{
  "scripts": {
    "dev": "webpack-dev-server --env.env=dev",
    "test": "karma start"
  },
  "dependencies": {
    "angular": "1.6.6",
    "angular-route": "1.6.6",
    "jquery": "^2.2.4",
    "moment": "~2.17.1",
    "bootstrap": "3.3.7",
    "lodash": "4.17.4",
    "@angular/common": "^5.2.5",
    "@angular/compiler": "^5.2.5",
    "@angular/core": "^5.2.5",
    "@angular/forms": "^5.2.5",
    "@angular/platform-browser": "^5.2.5",
    "@angular/platform-browser-dynamic": "^5.2.5",
    "@angular/router": "^5.2.5",
    "@angular/upgrade": "^5.2.5",
    "angular": "1.6.6",
    "angular-route": "1.6.6",
    "bootstrap": "3.3.7",
    "core-js": "^2.5.3",
    "jquery": "^2.2.4",
    "lodash": "4.17.4",
    "moment": "~2.17.1",
    "reflect-metadata": "^0.1.12",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.20"
  },
  "devDependencies": {
    "@types/angular": "^1.6.39",
    "@types/node": "^8.0.53",
    "angular-mocks": "^1.6.7",
    "autoprefixer": "^7.1.6",
    "css-loader": "^0.28.7",
    "extract-text-webpack-plugin": "^3.0.2",
    "file-loader": "^1.1.5",
    "html-webpack-plugin": "^2.30.1",
    "jasmine-core": "^2.8.0",
    "karma": "^1.7.1",
    "karma-jasmine": "^1.1.0",
    "karma-phantomjs-launcher": "^1.0.4",
    "karma-spec-reporter": "0.0.31",
    "karma-webpack": "^2.0.6",
    "node-sass": "^4.7.2",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "phantomjs-prebuilt": "^2.1.16",
    "postcss-loader": "^2.0.9",
    "raw-loader": "^0.5.1",
    "rimraf": "^2.6.2",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.19.0",
    "ts-loader": "^3.1.1",
    "typescript": "2.4.2",
    "webpack": "^3.3.0",
    "webpack-dev-server": "^2.9.5",
    "webpack-merge": "^4.1.1"
  }
}

app.module.ajs看起来像(跳过了导入):

export default angular.module('app', [
mainApp.name,
customers.name,
orders.name
])

但是这个主应用程序没有被渲染。它不会引发任何错误。如果我添加一些其他静态内容,它将显示该静态内容,而不显示角度分量。

请帮助我找出我在做错什么。

1 个答案:

答案 0 :(得分:0)

您直接复制/粘贴了index.html吗?如果是这样,则脚本源文件src=""javascripts...带有双引号,应该是单引号

  <script  src="javascripts/main.js">
  <script  src="javascripts/vendor.js">
  <body>
      <main-app></main-pp>

  </body>

否则,查看您的.ajs模块和package.json文件可能有助于诊断