如何使用Angular2作为非SPA?

时间:2016-06-10 12:17:39

标签: angular

我刚刚开始使用Angular2,发现可以在网上找到的所有教程都只解释了如何为单页应用程序(SPA)设置Angular2。

但是对于我网站的主页,我仍然想要使用PHP,只需在登录/注册/联系等几个地方使用Angular2。

如何初始化联系页面上的联系人组件,登录页面上的登录组件等?

我不希望所有这些都立即加载,只是一个处理登录,注册等功能的简单组件。

我相信需要在Bootstrapping部分完成某些事情,但究竟是什么?

编辑:我也可能想在一个页面上使用Login + Signup组件。

2 个答案:

答案 0 :(得分:7)

我认为您可以在该页面上引导您需要的任何组件

import {bootstrap}    from '@angular/platform-browser-dynamic';
import {ContactComponent} from './contact.component';

bootstrap(ContactComponent);

答案 1 :(得分:0)

请考虑以下示例,以便在同一客户端上具有多个页面:

每个客户都是一个有角度的SPA项目。 这意味着,客户端拥有1个index.html,它将根据参数路由到正确的spa页面。

https://embed.plnkr.co/plunk/eSq44S

Index.html文件:

    <!DOCTYPE html>
<html>

<head>
    <title>Home Page</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-inverse">
    <div class="container">
        <div class="navbar-header">
            <span class="navbar-brand">Just Components</span>
        </div>
        <div id="navbar">
            <ul class="nav navbar-nav">
                <li class="active"><a href="index.html">Home</a></li>
                <li><a href="items.html">Items</a></li>
                <li><a href="shapes.html">Shapes</a></li>
                <li><a href="items_and_shapes.html">Items and Shapes</a></li>
                <li><a href="other_static.html">Challenges/Consequences</a></li>
            </ul>
        </div>
    </div>
</nav>

<div class="container">
    <div>
        <h1>Angular 2 Components Without a SPA</h1>
        <table class="table table-bordered lead">
            <tr><td class="col-xs-3 text-right">Home</td><td>There is no angular on this page at all.</td></tr>
            <tr><td class="col-xs-3 text-right">Items</td><td>A static page with Component. Send data <em>OUT</em> of Component <em>TO</em> static page.</td></tr>
            <tr><td class="col-xs-3 text-right">Shapes</td><td>A static page with Component. Send data <em>INTO</em> Component <em>FROM</em> static page.</td></tr>
            <tr><td class="col-xs-3 text-right">Items & Shapes</td><td>Combine everything into one static page.</td></tr>
            <tr><td class="col-xs-3 text-right">Challenges/Consequences</td><td>for consideration, but not insurmountable</td></tr>
        </table>
    </div>
    <div>
        <h4>Why?</h4>
        <p>I'm asked routinely if you can put Angular 2 Components on a static page without creating a SPA.</p>
        <p>This is the answer: Yes</p>
        <h4>How?</h4>
        <p>This little app is an <em>imperfect</em> demonstration.</p>
        <p>Forget everything about @NgModel</p>
        <p>Forget everything about System.config path and app settings!</p>
        <p>Forget everything about tradtional Angular 2 bootstrapping!</p>
        <p>Each Component stands alone and boots itself</p>
    </div>

</div>
</body>
</html>

Items.html:

<!DOCTYPE html>
<html>

<head>
    <title>Static Pages with Angular 2 Components</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
    <script src="https://code.angularjs.org/tools/system.js"></script>
    <script src="https://code.angularjs.org/tools/typescript.js"></script>
    <script src="a2comps/config.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.0/angular2.min.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.0/http.min.js"></script>
    <script src="non_angular.js"></script>
    <script>
      System.import('a2comps/items.ts')
        .catch(console.error.bind(console));
    </script>
</head>

<body>
<nav class="navbar navbar-inverse">
    <div class="container">
        <div class="navbar-header">
            <span class="navbar-brand">Just Components</span>
        </div>
        <div id="navbar">
            <ul class="nav navbar-nav">
                <li><a href="index.html">Home</a></li>
                <li class="active"><a href="items.html">Items</a></li>
                <li><a href="shapes.html">Shapes</a></li>
                <li><a href="items_and_shapes.html">Items and Shapes</a></li>
                <li><a href="other_static.html">Challenges/Consequences</a></li>
            </ul>
        </div>
    </div>
</nav>

<div class="container">
    <div class="container">
        <div class="row">
            <div class="col-xs-8">
                <items>
                    <div class="text-center"><img src="gears.svg"></div>
                </items>
            </div>
            <div class="col-xs-4">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">Send Data OUT of a Component</h3>
                    </div>
                    <div class="panel-body">
                        <p>Click any row in a table. The row's data will be placed below.</p>
                        <br>
                        <p>The Angular Component generates a custom event. Plain old JS is used to listen for the custom event and update the view traditionally.</p>
                        <br>
                        <div id="itemizer"></div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
</body>

</html>

Items.ts:

import {bootstrap, BROWSER_PROVIDERS, BROWSER_APP_PROVIDERS} from "angular2/platform/browser";
import {Component} from "angular2/core"
import {SimpleTable} from "./simple_table.ts"

@Component({
    selector: "items",
    template: `
    <simple-table [contentTitle]="contentTitle" [tableContent]="items"></simple-table>
  `,
    directives: [SimpleTable]
})

export class Items {
    contentTitle = "Items & Prices";
    items = ITEMS;
}
bootstrap(Items);


//========================
// MOCK DATA
const ITEMS: Item[] = [
    {
    "id": 0,
    "name": "Item 0",
    "price": "$0"
},
    {
        "id": 1,
        "name": "Item 1",
        "price": "$1"
    },
    {
        "id": 2,
        "name": "Item 2",
        "price": "$2"
    },
    {
        "id": 3,
        "name": "Item 3",
        "price": "$3"
    },
    {
        "id": 4,
        "name": "Item 4",
        "price": "$4"
    },
    {
        "id": 5,
        "name": "Item 5",
        "price": "$5"
    }
];