使用ui-router的基于组件的路由| AngularJS

我们使用angularjs@1.6和angular-ui-router@1.0.0-rc.1。创建了一个名为contact的组件。问题在于单击按钮,主页上什么都没有显示。如果有人可以帮助我们找出错误。

index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
    <meta charset="utf-8">
    <title>Component based routing</title>
    <!--Styles-->

    <!--Libs-->
    <script src="../libs/angular.js"></script>
    <script src="../libs/angular-ui-router.js"></script>

    <!--Components-->
    <script src="app.js"></script>
    <script src="contact/contact.js"></script>
    <script src="contact/contact.component.js"></script>

</head>

<body>
    <h1>Component based routing</h1>
    <div ng-app="app">
        <ul>
            <li>
                <a href="#/contact">Contact</a>
            </li>
        </ul>
        <h4>Test</h4>
        <ui-view></ui-view>
    </div>
</body>

</html>

app.js

angular.module('app',[]);

contact.js

angular
    .module('contactApp',['ui-router']);

contact.component.js

angular
    .module('contactApp')
    .config(['$stateProvider',function ($stateProvider) {
        $stateProvider.state('contact',{
                url: '/contact',component: 'contact'
            })
    }])
    .component('contact',{
        template: `<h1>Contact</h1>`
    })
xiaoxingxing871019 回答:使用ui-router的基于组件的路由| AngularJS

您注册了组件的应用模块为contactApp。您需要将html更改为该应用程序模块:

<div ng-app="contactApp">

编辑

如果我们假设app是主应用程序模块,并且声明了其他模块,则所有其他模块都必须在主应用程序下注册,如下所示:

angular.module('app',['contactApp']);

,然后像以前一样,在HTML中使用app

<div ng-app="app">

*您也可以在同一html中拥有多个应用,但这不是推荐的方式

本文链接:https://www.f2er.com/3058605.html

大家都在问