简体   繁体   English

Bootstrapping Hybrid Angular 1 + 2应用程序

[英]Bootstrapping Hybrid Angular 1+2 Application

Following the official 5 min QuickStart I have a simple Angular 2 app working. 继官方5分钟QuickStart后,我有一个简单的Angular 2应用程序。

I set up Angular 1 and have now them both working independently, see this plunker . 我设置了Angular 1并且现在他们都独立工作,看到这个plunker

Following the official upgrade guide they say: 按照官方升级指南,他们说:

To then switch the application into hybrid mode, we must first install Angular 2 to the project. 要将应用程序切换到混合模式,我们必须首先将Angular 2安装到项目中。 Follow the instructions in the QuickStart for some pointers on this. 按照快速入门中的说明进行操作,以获得一些指示。 When we have Angular 2 installed, we can import and instantiate the UpgradeAdapter , and then call its bootstrap method. 当我们安装了Angular 2时,我们可以导入并实例化UpgradeAdapter ,然后调用它的bootstrap方法。 It is designed to take the exact same arguments as angular.bootstrap so that it is easy to make the switch: 它被设计为采用与angular.bootstrap完全相同的参数,以便很容易进行切换:

 import {UpgradeAdapter} from 'angular2/upgrade'; /* . . . */ const upgradeAdapter = new UpgradeAdapter(); upgradeAdapter.bootstrap(document.body, ['heroApp'], {strictDi: true}); 

My question is where to place it ? 我的问题是在哪里放置它

It sounds like a 'do my job' question but it's not so. 这听起来像是“做我的工作”问题,但事实并非如此。 I guess in main.ts , but I tried a lot of things without success. 我想在main.ts ,但是我尝试了许多没有成功的事情。 The doc is not really clear about this and some tutorials of 1 month old are already outdated. 该文档对此并不十分清楚,1个月的一些教程已经过时了。

--- update: ---更新:

In my case I forgot to load upgrade.js that I thought already included 在我的情况下,我忘了加载我认为已经包含的upgrade.js

I had a look at your plunkr. 我看了看你的傻瓜。 In fact you can use several times bootstrap but this means that you use two different applications into your HTML page. 实际上,您可以使用多次bootstrap但这意味着您在HTML页面中使用了两个不同的应用程序。

If you want to mix Angular1 and Angular2 stuff into the same application, you need to call only the boostrap method of the UpgradeAdapter . 如果你想Angular1和Angular2东西混合到同一个应用程序,你需要调用只有boostrap的方法UpgradeAdapter

The call of the boostrap function on the UpgradeAdapter and it's creation could be done in the boot.ts file: 的的呼叫boostrap功能上UpgradeAdapter和它的创建可以在完成boot.ts文件:

import {UpgradeAdapter} from 'angular2/upgrade';

var upgrade = new UpgradeAdapter();

// Configure the upgrade adapter
(...)

export function main() {
  try {
    upgrade.bootstrap(document.body, ['heroApp']);
  } catch (e) {
    console.error(e);
  }
}

You can import this into your HTML file like this: 您可以将其导入HTML文件,如下所示:

<script>
  System.import('app/main').then(function(src) {
    src.main();
  });
</script>

Mixing Angular2 and Angular1 stuff 混合Angular2和Angular1的东西

If you want to bootstrap an Angular2 application , provide the main component as first parameter and the provider for the Angular1 application to be able to use factories / services / directives into the Angular2 application. 如果要引导Angular2应用程序 ,请提供主要组件作为第一个参数,并提供Angular1应用程序的提供程序,以便能够将工厂/服务/指令用于Angular2应用程序。

// Providers for Angular1 elements
upgrade.upgradeNg1Provider('customService');
upgrade.upgradeNg1Provider('customFactory');

// Providers for Angular2 elements
upgrade.addProvider(HTTP_PROVIDERS);

// Bootstrap Angular2 application
upgrade.bootstrap(AppComponent);

If you want an Angular1 application to use Angular2 stuff , you need to provide the document.element as first parameter of the bootstrap method: 如果你想让Angular1应用程序使用Angular2 ,你需要提供document.element作为bootstrap方法的第一个参数:

// Providers for Angular2 elements
upgrade.downgradeNg2Provider(HTTP_PROVIDERS);

upgrade.bootstrap(document.body, ['heroApp']);

This plunkr could also be useful to you: http://plnkr.co/edit/yMjghOFhFWuY8G1fVIEg?p=preview . 这个plunkr也可能对你有用: http ://plnkr.co/edit/yMjghOFhFWuY8G1fVIEg?p = preview。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM